Tuesday, November 5, 2024

JavaScript: Redirecting URL

Whenever you want to generate dynamic pages you often have to resort to using some server side scripting language such as PHP or ASP.

But let us assume that there are only three or four pages that could be chosen from a drop-down menu (a combo-box). The following procedure helps you create a JavaScript-supported drop-down menu.

First the form:

<form name=="pages">
<select name=="pg_choice" onChange=="javascript: take_there();">
<option value=="no_page">Select a page to go to...</option>
<option value=="pg1.html">Page 1</option>
<option value=="pg2.html">Page 2</option>
<option value=="pg3.html">Page 3</option>
</select>
</form>

This form presents to you three choices of pages. In its onChange attribute we decide what function to call to be re-directed to the respective page. And now the JavaScript (note that I have inserted a dot between <.script language==”javascript”>

function take_there()
{
&nbsp&nbsp&nbsp var destination==document.pages.pg_choice.value;
&nbsp&nbsp&nbsp var version == navigator.appVersion;
&nbsp&nbsp&nbsp // sets variable == browser version
&nbsp&nbsp&nbsp if(destination!==”no_page”)
&nbsp&nbsp&nbsp {
&nbsp&nbsp&nbsp &nbsp if (version.indexOf(“MSIE”) >== -1)
&nbsp&nbsp&nbsp &nbsp // checks to see if using IE
&nbsp&nbsp&nbsp &nbsp {
&nbsp&nbsp&nbsp &nbsp &nbsp&nbsp window.location.href=stination;
&nbsp&nbsp&nbsp &nbsp }
&nbsp&nbsp&nbsp &nbsp else
&nbsp&nbsp&nbsp &nbsp {
&nbsp&nbsp&nbsp &nbsp &nbsp&nbsp window.open(destination, target==”_self”);
&nbsp&nbsp&nbsp &nbsp }
&nbsp&nbsp&nbsp }
}

</script>

Different browsers support different things so quite often you have to check what browser the user is using. The page redirection command is used accordingly.

Amrit Hallan is a freelance copywriter,
and a website content writer. He also dabbles
with PHP and HTML. For more tips and tricks in
PHP, JavaScripting, XML, CSS designing and
HTML, visit his blog at
http://www.aboutwebdesigning.com

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles