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 10-07-2012, 09:20 AM   PM User | #1
vilhelm
New to the CF scene

 
Join Date: May 2012
Posts: 8
Thanks: 2
Thanked 0 Times in 0 Posts
vilhelm is an unknown quantity at this point
Smile 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
vilhelm is offline   Reply With Quote
Old 10-07-2012, 04:11 PM   PM User | #2
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 453 Times in 451 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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...
xelawho is offline   Reply With Quote
Old 10-07-2012, 09:39 PM   PM User | #3
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 959
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
Allowing for missing parameter:
Code:
var car=window.location.search.substring(1).split("=");

if ( car.length == 2 && car[ 1 ] == "mercedes" )
{
  alert("do stuff")
}
Logic Ali is offline   Reply With Quote
Old 10-07-2012, 09:56 PM   PM User | #4
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 453 Times in 451 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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?
xelawho is offline   Reply With Quote
Old 10-07-2012, 10:27 PM   PM User | #5
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 959
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
Quote:
Originally Posted by xelawho View Post
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.
Logic Ali 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 07:48 PM.


Advertisement
Log in to turn off these ads.