Javascript: Setting variable to work in attr line
Kinda of a noob question, can't seem to solve it, but I may just be tired.
What I'm trying to do is insert a PHP variable in to Javascript (Jquery), and then that variable in to an attr line.
The script I'm using to create the variable:
$.extend({
getUrlVars: function(){
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return 开发者_运维知识库vars;
},
getUrlVar: function(name){
return $.getUrlVars()[name];
}
});
Setting the variable:
var searchresult = $.getUrlVar('s');
And then, where I'm stuck, inserting the variable within the line so that'll come out correctly when I send it off to my iframe.
$(function() {
$("#searchbutton").click(function() {
$("#theiframe").attr("src","http://whateverdomain.com/index.php?s=VARIABLE HERE WITHIN JAVASCRIPT");
})
})
I know, kinda beginners question going here, but I admit it, I'm stuck.
$(function() {
$("#searchbutton").click(function() {
var searchresult = getUrlVar("s");
$("#theiframe").attr("src", "http://example.com/index.php?s=" + searchresult);
})
})
There's already a jQuery plugin that parses URL query parameters - really no need to write your own.* Check out the example page for parsing query parameters.
*Though it looks like you didn't actually write the getUrlVars
plugin yourself.
精彩评论