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 01-05-2012, 04:18 PM   PM User | #1
chickentulip
New Coder

 
Join Date: Oct 2010
Location: Toronto
Posts: 95
Thanks: 52
Thanked 0 Times in 0 Posts
chickentulip is an unknown quantity at this point
script ignores the change in the select menu

the script below should open a new window with a url specified in the select area. However, my script ignores the changes in the select area. it keeps opening the same page. I was wondering could someone take a look and tell me why?
Thank you very much.

<code>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> Jump Menu</title>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<script type="text/javascript">
function jump_menu() {
var i=document.getElementById("page_select").selectedIndex;
document.getElementById("jump_to_page").onclick=function() {
(i==0) ? alert("Please select a page to go"):window.open("http://www."+document.getElementById("page_select").options[i].value);
}
}

window.onload=jump_menu;
</script>
</head>
<body>
<select id="page_select">
<option>Select a Page</option>
<option value="yahoo.ca">Yahoo</option>
<option value="topnews.ru">Top News</option>
<option value="google.com">Google</option>
<option value="amazon.com">Amazon</option>
</select>
<button type="button" id="jump_to_page">Go</button>

</body>
</html>

</code>
chickentulip is offline   Reply With Quote
Old 01-05-2012, 04:47 PM   PM User | #2
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
this seems to work better... don't like that alert on window load much though

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> Jump Menu</title>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<script type="text/javascript">

function openPage() {
var i=document.getElementById("page_select").selectedIndex;
(i==0) ? alert("Please select a page to go"):window.open("http://www."+document.getElementById("page_select").options[i].value);
}

window.onload=alert("Please select a page to go")
</script>
</head>
<body>
<select id="page_select">
<option>Select a Page</option>
<option value="yahoo.ca">Yahoo</option>
<option value="topnews.ru">Top News</option>
<option value="google.com">Google</option>
<option value="amazon.com">Amazon</option>
</select>
<button type="button" id="jump_to_page" onclick="openPage()">Go</button>

</body>
</html>
xelawho is offline   Reply With Quote
Old 01-05-2012, 05:02 PM   PM User | #3
chickentulip
New Coder

 
Join Date: Oct 2010
Location: Toronto
Posts: 95
Thanks: 52
Thanked 0 Times in 0 Posts
chickentulip is an unknown quantity at this point
Thank you, xelawho.

the idea was not to use any inline-scripting. Plus, I dont want an alert message after the window is loaded.

It seems the script has some kind of logical error.
chickentulip is offline   Reply With Quote
Old 01-05-2012, 05:09 PM   PM User | #4
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
how about this?

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> Jump Menu</title>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />

</head>
<body>
<select id="page_select">
<option>Select a Page</option>
<option value="yahoo.ca">Yahoo</option>
<option value="topnews.ru">Top News</option>
<option value="google.com">Google</option>
<option value="amazon.com">Amazon</option>
</select>
<button type="button" id="jump_to_page">Go</button>
<script type="text/javascript">
document.getElementById("jump_to_page").onclick=function() {
var i=document.getElementById("page_select").selectedIndex;
(i==0) ? alert("Please select a page to go"):window.open("http://www."+document.getElementById("page_select").options[i].value);
}
</script>
</body>
</html>
</code>
xelawho is offline   Reply With Quote
Users who have thanked xelawho for this post:
chickentulip (01-05-2012)
Old 01-05-2012, 05:15 PM   PM User | #5
chickentulip
New Coder

 
Join Date: Oct 2010
Location: Toronto
Posts: 95
Thanks: 52
Thanked 0 Times in 0 Posts
chickentulip is an unknown quantity at this point
thank you!. it is working. I will study you code. I still don't understand why my code did not produce the result i was after.
chickentulip is offline   Reply With Quote
Old 01-05-2012, 05:31 PM   PM User | #6
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
actually your script was pretty close - you only needed to move this line:
Code:
var i=document.getElementById("page_select").selectedIndex;
inside the onclick function so that the selectedIndex variable changed every time the button was clicked.

I moved it around because all the jump_menu function was doing in the end was setting the onclick for the button on window load - which gets done anyway if you just leave it at the start of the script (but after the html elements have been defined)
xelawho is offline   Reply With Quote
Old 01-05-2012, 05:54 PM   PM User | #7
chickentulip
New Coder

 
Join Date: Oct 2010
Location: Toronto
Posts: 95
Thanks: 52
Thanked 0 Times in 0 Posts
chickentulip is an unknown quantity at this point
Thank you. yes, i noticed myself that it is a matter of placing the line that you talking about as the first line in the anonymous function to make my original script working.
i had no idea that the position of this line in the code would matter
chickentulip 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 01:05 PM.


Advertisement
Log in to turn off these ads.