CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript programming (http://www.codingforums.com/forumdisplay.php?f=2)
-   -   Reach IF statment from address field (http://www.codingforums.com/showthread.php?t=275558)

vilhelm 10-07-2012 09:20 AM

Reach IF statment from address field
 
Hi!
Is there any way to reach a IF ELSE statment via the address filed?

Like there is with the GET method for PHP?

For example index.html?car=mercedes writes or reqiure a file, that should have been viewed if the variable cars value was "mercedes"? :)

/ Vilhelm

xelawho 10-07-2012 04:11 PM

you can check the value of the query string like this:
Code:

var car=window.location.search.substring(1).split("=")[1];
if (car=="mercedes"){
alert("do stuff")
}

... the "do stuff" bit I leave to you... :D

Logic Ali 10-07-2012 09:39 PM

Allowing for missing parameter:
Code:

var car=window.location.search.substring(1).split("=");

if ( car.length == 2 && car[ 1 ] == "mercedes" )
{
  alert("do stuff")
}


xelawho 10-07-2012 09:56 PM

fair enough. seems overly cautious though... if the string is index.html?car=

then window.location.search.substring(1).split("=")[1]

will return blank, which obviously != "mercedes", so why?

Logic Ali 10-07-2012 10:27 PM

Quote:

Originally Posted by xelawho (Post 1277338)
fair enough. seems overly cautious though... if the string is index.html?car=

then window.location.search.substring(1).split("=")[1]

will return blank, which obviously != "mercedes", so why?

I just don't like to see the practice of assuming the existence of an object or element, even if it's not fatal.


All times are GMT +1. The time now is 05:52 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.