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.
Page 1 of 3 123 LastLast
Results 1 to 15 of 41
  1. #1
    New Coder
    Join Date
    Mar 2009
    Posts
    88
    Thanks
    10
    Thanked 0 Times in 0 Posts

    browserName=navigator.appName not working

    Hello all,

    I have this code and I don't understand why this isn't working.

    Head section:
    Code:
    <script>
    function IEorMoz(){
    
    // get browser resolution
    var browserName=navigator.appName;
    if (browserName=="Netscape"){
    
    // Netscape/FireFox
    document.write('<div class='s6l_short_url_form6' id='uitkomst2'><a href='javascript:document.write('<html><head><title>{$txt_short_url}</title></head>
    <body><form name=\'urlform\' action=\'{$short_url}\' method=\'post\'><input type=\'hidden\' name=\'u\' value=\'' + window.location.href + '\' /></form></body></html>');document.forms[0].submit();'><img src='../template/default/image/slepen.png' alt='Twit This!' width='171' height='44' border='0' align='top'></a></div>');
    
    } else {
    
    // IE
    document.write('<div class='s6l_short_url_form6' id='uitkomst2'><a href='javascript:document.write('<html><head><title>{$txt_short_url}</title></head>
    <body><form name=\'urlform\' action=\'{$short_url}\' method=\'post\'><input type=\'hidden\' name=\'u\' value=\'' + window.location.href + '\' /></form></body></html>');document.forms[0].submit();'><img src='../template/default/image/slepen.png' alt='Twit This!' width='171' height='44' border='0' align='top'></a></div>');
    }
    }
    </script>
    Body section:
    Code:
    <div onclick='IEorMoz();'>get res</div>
    Thanks a ton in advance
    ps I know the 2 div's are the same, but this is only for testing.
    Last edited by boemboem; 06-16-2009 at 05:38 PM.

  • #2
    New Coder
    Join Date
    Mar 2009
    Posts
    88
    Thanks
    10
    Thanked 0 Times in 0 Posts
    I have also tries this instead of the

    Code:
    <script type = "text/javascript">
    function getres(){
    
    // get browser resolution
    var browserName=navigator.appName;
    if (browserName=="Netscape"){
    
    // Netscape/FireFox
    document.write("<div class=\"s6l_short_url_form6\" id=\"uitkomst2\"><a href=\"javascript:document.write(\'<html><head><title>{$txt_short_url}<\/title><\/head>");
    document.write("<body><form name=\\'urlform\\' action=\\'{$short_url}\\' method=\\'post\\'><input type=\\'hidden\\' name=\\'u\\' value=\\'\' + window.location.href + \'\\' \/><\/form><\/body><\/html>\');document.forms[0].submit();\"><img src=\"..\/template\/default\/image\/slepen.png\" alt=\"Twit This!\" width=\"171\" height=\"44\" border=\"0\" align=\"top\"><\/a><\/div>");
    
    } else {
    
    // IE
    document.write("<div class=\"s6l_short_url_form6\" id=\"uitkomst2\"><a href=\"javascript:document.write(\'<html><head><title>{$txt_short_url}<\/title><\/head>");
    document.write("<body><form name=\\'urlform\\' action=\\'{$short_url}\\' method=\\'post\\'><input type=\\'hidden\\' name=\\'u\\' value=\\'\' + window.location.href + \'\\' \/><\/form><\/body><\/html>\');document.forms[0].submit();\"><img src=\"..\/template\/default\/image\/slepen.png\" alt=\"Twit This!\" width=\"171\" height=\"44\" border=\"0\" align=\"top\"><\/a><\/div>");
    }
    }
    </script>

  • #3
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Quote Originally Posted by boemboem View Post
    ps I know the 2 div's are the same, but this is only for testing.
    Then how do you know it's not working?
    Are you a Help Vampire?

  • #4
    New Coder
    Join Date
    Mar 2009
    Posts
    74
    Thanks
    3
    Thanked 3 Times in 3 Posts
    It would probably work better to store the stuff you're going to write in a var, so do something like this:

    Code:
    var newpage=''
    newpage+='lorem ipsum<a href="link.html">lorem ipsum</a>'
    newpage+='more text here'
    ...
    document.write(newpage)
    I think another issue is escaping characters. If you start and end a javascript string with a single quote and use double quotes (or escaped single quotes) inside it usually is safer.

    I see that you're trying to write single quotes inside of other single quotes, and if you do that you have to escape it like this:

    document.write('<a href="javascript:myfunction(\'myfirstvar\')">lorem ipsum</a>')

  • #5
    New Coder
    Join Date
    Mar 2009
    Posts
    88
    Thanks
    10
    Thanked 0 Times in 0 Posts
    I try to convert a excisting div into a document.write("");
    Maybe the code before the document.write is not good?

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    document.write statements must be run before the page finishes loading. Any document.write statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page. So document.write is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.

  • Users who have thanked Philip M for this post:

    tomws (06-16-2009)

  • #7
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Detecting browsers by their navigator.appName is not reliable. Browsers do often spoof their name. You should rather detect a certain contested method (and code accordingly).
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #8
    New Coder
    Join Date
    Mar 2009
    Posts
    88
    Thanks
    10
    Thanked 0 Times in 0 Posts
    I'm a bit confused now, so I tried something easier, it still doesn't work
    Can somebdy tell me how I can mak this work?

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function writeJS(){
    var str='';
    str+='<a href="javascript:document.write('<html><head><title>{$txt_short_url}</title></head>
    <body><form name=\'urlform\' action=\'{$short_url}\' method=\'post\'><input type=\'hidden\' name=\'u\' value=\'' + window.location.href + '\' /></form></body></html>');document.forms[0].submit();"><img src="..\/template\/default\/image\/slepen.png" width="171" height="44" border="0"><\/a>';
    str+='To be or not to be!';
    document.write(str);
    }
    
    </script>
    </head>
    <body onLoad="writeJS()";>
    
    If there is an image, it works ok!
    
    </body>
    </html>

  • #9
    New Coder
    Join Date
    Mar 2009
    Posts
    88
    Thanks
    10
    Thanked 0 Times in 0 Posts
    At the moment I got this working

    Code:
    <script type="text/javascript">
    function writeJS(){
    var browserName=navigator.appName;
    if (browserName=="Netscape")
    {
    document.writeln("<div class=\"s6l_short_url_form6\" id=\"uitkomst2\"><a href=\"javascript:document.write(\'<html><head><title>{$txt_short_url}<\/title><\/head>");
    document.writeln("<body><form name=\\'urlform\\' action=\\'{$short_url}\\' method=\\'post\\'><input type=\\'hidden\\' name=\\'u\\' value=\\'\' + window.location.href + \'\\' \/><\/form><\/body><\/html>\');document.forms[0].submit();\"><img src=\"http:\/\/luxsci.com\/blog\/wp-content\/uploads\/2009\/02\/firefox-logo.png\" alt=\"Twit This!\" width=\"171\" height=\"44\" border=\"0\" align=\"top\"><\/a><\/div>");
    }
    else
    {
     if (browserName=="Microsoft Internet Explorer")
     {
    document.writeln("<div class=\"s6l_short_url_form6\" id=\"uitkomst2\"><a href=\"javascript:document.write(\'<html><head><title>{$txt_short_url}<\/title><\/head>");
    document.writeln("<body><form name=\\'urlform\\' action=\\'{$short_url}\\' method=\\'post\\'><input type=\\'hidden\\' name=\\'u\\' value=\\'\' + window.location.href + \'\\' \/><\/form><\/body><\/html>\');document.forms[0].submit();\"><img src=\"http:\/\/blogs.nitobi.com\/alexei\/wp-content\/uploads\/2008\/03\/internet_explorer_7_logo.png\" alt=\"Twit This!\" width=\"171\" height=\"44\" border=\"0\" align=\"top\"><\/a><\/div>");
     }
     }
    </script>
    
    <body onLoad="writeJS()";>
    Can somebody tell me how I can push this in a div with GetElementByID??
    Some handles are welcome please.

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by boemboem View Post
    [/CODE]

    Can somebody tell me how I can push this in a div with GetElementByID??
    Some handles are welcome please.
    Javascript is case-sensitive.

    document.getElementbyId("mydiv").innerHTML = whatever

    Perhps it might be an idea to buy an introductory textbook on Javascript.

  • #11
    New Coder
    Join Date
    Mar 2009
    Posts
    88
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Thank you for your reply. Buying a Javascript book (for Dummy's would be a good idea, I will look for that. I can understand that you don't want to create my wishes, but I do my best to understand javascript, when I post a message here, I do that because I got stuck somewhere. With some handles I can try further.

    Is it possible to put a complete document.write in a document.getElementbyId("mydiv").innerHTML like:

    Code:
    document.getElementbyId("mydiv").innerHTML = document.writeln("<div class=\"s6l_short_url_form6\" id=\"uitkomst2\"><a href=\"javascript:document.write(\'<html><head><title>{$txt_short_url}<\/title><\/head>");
    document.writeln("<body><form name=\\'urlform\\' action=\\'{$short_url}\\' method=\\'post\\'><input type=\\'hidden\\' name=\\'u\\' value=\\'\' + window.location.href + \'\\' \/><\/form><\/body><\/html>\');document.forms[0].submit();\"><img src=\"http:\/\/luxsci.com\/blog\/wp-content\/uploads\/2009\/02\/firefox-logo.png\" alt=\"Twit This!\" width=\"171\" height=\"44\" border=\"0\" align=\"top\"><\/a><\/div>");

  • #12
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by boemboem View Post
    Is it possible to put a complete document.write in a document.getElementbyId("mydiv").innerHTML like:

    Code:
    document.getElementbyId("mydiv").innerHTML = document.writeln("<div class=\"s6l_short_url_form6\" id=\"uitkomst2\"><a href=\"javascript:document.write(\'<html><head><title>{$txt_short_url}<\/title><\/head>");
    document.writeln("<body><form name=\\'urlform\\' action=\\'{$short_url}\\' method=\\'post\\'><input type=\\'hidden\\' name=\\'u\\' value=\\'\' + window.location.href + \'\\' \/><\/form><\/body><\/html>\');document.forms[0].submit();\"><img src=\"http:\/\/luxsci.com\/blog\/wp-content\/uploads\/2009\/02\/firefox-logo.png\" alt=\"Twit This!\" width=\"171\" height=\"44\" border=\"0\" align=\"top\"><\/a><\/div>");
    No. See post #6.

    Why do you want to do these strange things? What are you actually trying to achieve?

  • #13
    New Coder
    Join Date
    Mar 2009
    Posts
    88
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Hello again,

    What I actually want to achieve is pretty simple, but not for me, it gives me headaches

    I'll try to explain in my best english.

    1 - I have a image in a div, with a link in it. The html code for that one is this:

    Code:
    <div class="s6l_short_url_form6">
    <a href="javascript:document.write('<html><head><title>Short URL</title></head> <body><form name=\'urlform\' action=\'http://twiturl.nl/\' method=\'post\'><input type=\'hidden\' name=\'u\' value=\'' + window.location.href + '\' /></form>');document.forms[0].submit();"><img src="http://www.survivalbond.nl/images/news/duim.jpg" width="111" height="44" /></a>
    </div>
    2 - When somebody uses IE or Mozilla FF, a different picture has to be shown in a DIV, the code I have for that is. Wich actually works good.

    Code:
    <script type="text/javascript">
    function writeJS(){
    var browserName=navigator.appName;
    if (browserName=="Netscape")
    {
    document.writeln("<div class=\"s6l_short_url_form6\" id=\"uitkomst2\"><a href=\"javascript:document.write(\'<html><head><title>{$txt_short_url}<\/title><\/head>");
    document.writeln("<body><form name=\\'urlform\\' action=\\'{$short_url}\\' method=\\'post\\'><input type=\\'hidden\\' name=\\'u\\' value=\\'\' + window.location.href + \'\\' \/><\/form><\/body><\/html>\');document.forms[0].submit();\"><img src=\"http:\/\/luxsci.com\/blog\/wp-content\/uploads\/2009\/02\/firefox-logo.png\" alt=\"Twit This!\" width=\"171\" height=\"44\" border=\"0\" align=\"top\"><\/a><\/div>");
    }
    else
    {
     if (browserName=="Microsoft Internet Explorer")
     {
    document.writeln("<div class=\"s6l_short_url_form6\" id=\"uitkomst2\"><a href=\"javascript:document.write(\'<html><head><title>{$txt_short_url}<\/title><\/head>");
    document.writeln("<body><form name=\\'urlform\\' action=\\'{$short_url}\\' method=\\'post\\'><input type=\\'hidden\\' name=\\'u\\' value=\\'\' + window.location.href + \'\\' \/><\/form><\/body><\/html>\');document.forms[0].submit();\"><img src=\"http:\/\/blogs.nitobi.com\/alexei\/wp-content\/uploads\/2008\/03\/internet_explorer_7_logo.png\" alt=\"Twit This!\" width=\"171\" height=\"44\" border=\"0\" align=\"top\"><\/a><\/div>");
     }
     }
    }
    </script>
    3 - The problem now is that I don't know how to call the function writeJS() in a way that it doesn't overwrite the whole page like you said in #6
    I'm out of ideas at the moment.

    Basicly in short. I want to have two different div's, containing two different images, the only shows in Internet Explorer, the other only in Mozilla FF
    Last edited by boemboem; 06-17-2009 at 05:14 PM.

  • #14
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    You're making it more complicated than it needs to be.

    Code:
    <div class="s6l_short_url_form6">
    <a href="javascript:document.write('<html><head><title>Short URL</title></head> <body><form name=\'urlform\' action=\'http://twiturl.nl/\' method=\'post\'><input type=\'hidden\' name=\'u\' value=\'' + window.location.href + '\' /></form>');document.forms[0].submit();"><img src="http://www.survivalbond.nl/images/news/duim.jpg" width="111" height="44" /></a>
    </div>

    Suggestions...

    First, you have a document.write as your link. That's going to do exactly what you don't want to happen. So, remove it.

    Second, why are you trying to write an entirely new page when the link is clicked? That also not what you say you want to happen. So, remove that.

    Point the href to the javascipt function only.

    To change the image, either:
    1) Give an ID to your div and make your function modify only the innerHTML of that div, or
    2) Give an ID to your image tag and make you function modify the .src of that tag.

    Don't use document.write* anywhere.
    Are you a Help Vampire?

  • Users who have thanked tomws for this post:

    boemboem (06-17-2009)

  • #15
    New Coder
    Join Date
    Mar 2009
    Posts
    88
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Tomws, thank you for your reply.

    The code needs to be like this because it's a bookmarklet. This is the original html code. The only difference will be the image shown to a IE user or FF user.

    Code:
    <a href="javascript:document.write('<html><head><title>{$txt_short_url}</title></head><body><form name=\'urlform\' action=\'{$page_url}\' method=\'post\'><input type=\'hidden\' name=\'u\' value=\'' + window.location.href + '\' /><input type=\'hidden\' name=\'title\' value=\''+encodeURIComponent(document.title)+'\' /><input type=\'hidden\' name=\'twitter\' value=\'1\' /></form></body></html>');document.forms[0].submit();" title="{$txt_bookmark_me}" class="s6l_bookmarklet"><img src="../default/image/slepen.png" width="171" height="44" border="0" /></a>
    At this point I can't see the forest though the trees


  •  
    Page 1 of 3 123 LastLast

    Posting Permissions

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