PDA

View Full Version : Reach IF statment from address field



vilhelm
10-07-2012, 10:20 AM
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, 05:11 PM
you can check the value of the query string like this:


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, 10:39 PM
Allowing for missing parameter:


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

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

xelawho
10-07-2012, 10: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, 11:27 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?

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