/    Sign up×
Community /Pin to ProfileBookmark

Getting variable from URL then use in jQuery

All,
I have the following code which is triggered on a change of a drop down box:

[CODE]
$(“#select_pics”).change(function(){
var pic_change = $(“#select_pics”).val();
$.post(“load_my_photos.php”, {show_pics: pic_change}, function(result) {
$(“#image_divs”).html(result);
});
});
[/CODE]

So the select to do do this currently looks like this:

[CODE]
<select name=”pics” id=”select_pics” class=”styled”><option value=”my_pics”>My Added Pics</option><option value=”finish_photos” selected>Finished Photos</option><option value=”shopping_cart_photos”>Shopping Cart Photos</option><option value=”purchased_photos”>Purchased Photos</option></select>
[/CODE]

So basically what I’d like to is have a URL like: [url]http://website.com/photo.php?pic_change=shopping_cart_photos[/url]

I’d like the script to take anything after the equal sign and basically execute the same jQuery code above but I don’t know how I’d call it because it wouldn’t be on a change, it would be during a load. Any ideas on how to do this? Thanks in advance!

UPDATE: I found the $(window).load function but that waits until the page is loaded. Is there anything I can use to do this before the page is actually loaded?

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@WolfShadeMar 07.2011 — If you try to run it before the page is done loading, then there is a very good chance that the SELECT element it is referring to won't be loaded, which will trigger an error.

^_^
Copy linkTweet thisAlerts:
@treeleaf20authorMar 07.2011 — That's ok. I think I can use the $(window).load() and go from there but I'm still struggling with getting the text after the equals sign with jQuery.
Copy linkTweet thisAlerts:
@rnd_meMar 08.2011 — That's ok. I think I can use the $(window).load() and go from there but I'm still struggling with getting the text after the equals sign with jQuery.[/QUOTE]


[CODE]
function parseQS(str){
var ob={}, float="", key="", dc=decodeURIComponent;

for( var i=0, mx=str.length; i<mx;i++){
var it=str[i];
if(it==="="){ key=float; float=""; continue;}
if(!it.search(/^[?&]/)){
if(it==="&" && str.slice(i+1,i+5)==="amp;"){ i=(i+4);float+="&"; continue;}
if(key){ob[key]=dc(float);} key=""; float=""; continue;
}
float+=it;
}
ob[key]=dc(float);
return ob;
}


//demo of URL term extraction:

var allParams=parseQS(location.href);
alert(allParams.pic_change) //alerts "shopping_cart_photos" from .?pic_change=shopping_cart_photos


[/CODE]
×

Success!

Help @treeleaf20 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...