Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-05-2012, 01:11 PM   PM User | #1
sebastiz
New to the CF scene

 
Join Date: May 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
sebastiz is an unknown quantity at this point
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>
sebastiz is offline   Reply With Quote
Old 05-05-2012, 01:50 PM   PM User | #2
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,764
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Where are you storing the results of the function?
jmrker is offline   Reply With Quote
Old 05-05-2012, 01:53 PM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,038
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 05-05-2012, 11:57 PM   PM User | #4
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,455
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Old 05-06-2012, 11:24 AM   PM User | #5
sebastiz
New to the CF scene

 
Join Date: May 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
sebastiz is an unknown quantity at this point
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
sebastiz is offline   Reply With Quote
Old 05-06-2012, 11:59 AM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,038
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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]>
__________________

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.

Last edited by Philip M; 05-06-2012 at 12:37 PM..
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:10 PM.


Advertisement
Log in to turn off these ads.