Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    regular expression to replace html depending on browser

    I want to change my html depending on what browser the user is using.
    I know the regular expression works as I've tested it endlessly.
    Can anyone help?
    Here is the code

    <script language="JavaScript"
    type="text/JavaScript">
    if(navigator.appName == "Microsoft Internet Explorer")
    {
    function replaceit(s) {
    s.replace(/.*data=\"(.+?).svg".*/g, " <!-- end .content --> <div class=\"content\" style=\"text-align: center;height: 1000px;\"><img src=\"$1.html\" width=\"1104\" height=\"111\" alt=\"Gastroenterology Book\" /></div>"); }

    }
    </script></head>

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,026
    Thanks
    36
    Thanked 494 Times in 488 Posts
    Where are you storing the results of the function?

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    function replaceit(s) { // You have not defined s

    I don't think that it is possible to replace HTML in that manner. What you can do is show/hide a <div> depending on the browser - but even so that is not a very reliable way of identifying the browser, where the appName can be spoofed. Prefer to use object detection.

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,459
    Thanks
    0
    Thanked 632 Times in 622 Posts
    Quote Originally Posted by sebastiz View Post
    if(navigator.appName == "Microsoft Internet Explorer")
    All browsers can have the appName set to that value. IE can have it set to anything at all.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    New to the CF scene
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK. So its better to refer to the object id which is fine.
    Basically my website has loads of SVG files, but because IE doesnt support SVG without plugins, I'd like the webpage to show the JPEG version of SVG. The SVG on the webpage is in an <object> tag whereas the JPEG obviously needs to be in an <img> tag.
    I thought the best way to do this was to use javascript to check the browser and then change the html to get the JPEG in an img tag. Am I approaching this the right way? Thanks in advance

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by sebastiz View Post
    OK. So its better to refer to the object id which is fine.
    Basically my website has loads of SVG files, but because IE doesnt support SVG without plugins, I'd like the webpage to show the JPEG version of SVG. The SVG on the webpage is in an <object> tag whereas the JPEG obviously needs to be in an <img> tag.
    I thought the best way to do this was to use javascript to check the browser and then change the html to get the JPEG in an img tag. Am I approaching this the right way? Thanks in advance
    I think you have had the answer, No. You could have a second page to which it redirects if the browser is identified as IE<=8, (IE9 does support SVG files) but you must use object detection or conditional comments to identify IE:-

    http://www.javascriptkit.com/javatut...jdetect3.shtml

    Code:
    <!--[if gte IE 8]>
    <p>You're using a recent version of Internet Explorer.</p>
    <script type = "text/javascript">
    alert ("You're using a recent version of Internet Explorer - SVG files are supported");
    </script>
    <![endif]-->
    
    <!--[if lt IE 9]>
    <script type = "text/javascript">
    alert ("You're using a older version of Internet Explorer which does not support SVG files.");
    window.location.href = "http://www.google.com";  // redirect to another page
    </script>
    <![endif]-->
    
    <![if !IE]>
    <p>You're not using Internet Explorer.</p>
    <![endif]>
    Last edited by Philip M; 05-06-2012 at 12:37 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •