new window? target="_blank" ?

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Oct 2005
Posts: 240
Reputation: Inny is an unknown quantity at this point 
Solved Threads: 6
Inny's Avatar
Inny Inny is offline Offline
Posting Whiz in Training

new window? target="_blank" ?

 
0
  #1
Feb 14th, 2007
I want random links in this code to open in a new window. how to please ?

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script language="JavaScript">
  2. <!--
  3. // Create arrays to contain all the values
  4. // for links and image locations
  5. link = new Array
  6. image = new Array
  7.  
  8. link[1]="http://www.wildexpo.com.au/Aust%20Rep%20Show/"
  9. image[1]="http://www.reptilesaustralia.com.au/forums/Ads/wildexpo.gif"
  10.  
  11. link[2]="http://www.excite.com"
  12. image[2]="http://www.excite.com/mesp/images/excite/new_logo-180.gif"
  13.  
  14. link[3]="http://www.lycos.com"
  15. image[3]="http://a284.g.akamai.net/7/284/987/000/lygo.com/ly/i/lyguide.gif"
  16.  
  17. link[4]="http://www.cnet.com"
  18. image[4]="http://cnet.com/Images/Headers/cnet-1-title.gif"
  19.  
  20. link[5]="http://www.webcrawler.com"
  21. image[5]="http://webcrawler.com/img/web/hdr/home_header.gif"
  22.  
  23.  
  24. // Create a random number between 1 and five
  25. random_num = (Math.round((Math.random()*4)+1))
  26.  
  27.  
  28. // Write a link and images with random array
  29. document.write("<a href=\"" + link[random_num] + "\">");
  30. document.write("<img src=\"" + image[random_num] + "\" border=\"0\"></a>");
  31.  
  32. -->
  33. </script>
Always carry a flagon of whiskey in case of snakebite and furthermore always carry a small snake.
W. C. Fields
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 1,091
Reputation: MattEvans is a jewel in the rough MattEvans is a jewel in the rough MattEvans is a jewel in the rough 
Solved Threads: 63
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Veteran Poster

Re: new window? target="_blank" ?

 
0
  #2
Feb 14th, 2007
target = "_blank" should work.

change:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. document.write("<a href=\"" + link[random_num] + "\">");
to
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. document.write("<a href=\"" + link[random_num] + "\" target=\"_blank\">");
Plato forgot the nullahedron..
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 240
Reputation: Inny is an unknown quantity at this point 
Solved Threads: 6
Inny's Avatar
Inny Inny is offline Offline
Posting Whiz in Training

Re: new window? target="_blank" ?

 
0
  #3
Feb 14th, 2007
Thanks Again Matt!

Resolved!
Always carry a flagon of whiskey in case of snakebite and furthermore always carry a small snake.
W. C. Fields
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 240
Reputation: Inny is an unknown quantity at this point 
Solved Threads: 6
Inny's Avatar
Inny Inny is offline Offline
Posting Whiz in Training

Re: new window? target="_blank" ?

 
0
  #4
Feb 14th, 2007
Not Quite Resolved! Whats wrong with back slash? The code works in testbed but wont cop the backslash on my board (ipb) ?

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. document.write("<a href=\"" + link[random_num] + "\" target=\"_blank\">");
  2.  
  3. document.write("<img src=\"" + image[random_num] + "\" border=\"0\"></a>");
Always carry a flagon of whiskey in case of snakebite and furthermore always carry a small snake.
W. C. Fields
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 1,091
Reputation: MattEvans is a jewel in the rough MattEvans is a jewel in the rough MattEvans is a jewel in the rough 
Solved Threads: 63
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is offline Offline
Veteran Poster

Re: new window? target="_blank" ?

 
0
  #5
Feb 14th, 2007
Originally Posted by Inny View Post
Not Quite Resolved! Whats wrong with back slash? The code works in testbed but wont cop the backslash on my board (ipb) ?

[/code]
Hmm... perhaps your board software is looking for escaped characters and eating them.. o_O

try either:

document.write("<a href=\\"" + link[random_num] + "\\" target=\\"_blank\\">");
document.write("<img src=\\"" + image[random_num] + "\\" border=\\"0\\"></a>");

(double backslash should be escaped to backslash by your board software which can then go on to be the javascript escape character when the page is rendered)

or (my prefered way):

document.write('<a href="' + link[random_num] + '" target="_blank">');
document.write('<img src="' + image[random_num] + '" border="0"></a>');

(all literal strings in the Javascript are quoted with single quote characters, and attributes in the dynamically created elements are quoted with double quote characters :- escaping doesn't matter)
Last edited by MattEvans; Feb 14th, 2007 at 2:53 pm.
Plato forgot the nullahedron..
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 240
Reputation: Inny is an unknown quantity at this point 
Solved Threads: 6
Inny's Avatar
Inny Inny is offline Offline
Posting Whiz in Training

Re: new window? target="_blank" ?

 
0
  #6
Feb 14th, 2007
Matt to the resue! Once again Thankyou mate!
Always carry a flagon of whiskey in case of snakebite and furthermore always carry a small snake.
W. C. Fields
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC