I have one function :
jQuery('#'+'linkhere').click(getHashPageClick);
and in the function I am retreving window.location.hash value :
var getHashPageClick = function(evt) {
var hashParam = window.location.hash.substring(1);
}
But now I want to send a parameter to getHashPageClick(), say value , append hashParam value to it and access it in my calling function ? So something like this, maybe? :
jQuery('#'+'linkhere').click(getHashPageClick(value);
alert("value");
and in the called function modify value of value :
var getHashPageClick = function(evt,value) {
var hashParam = window.location.hash.substring(1);
valuevalue+hashParam;
return value;
}
Would that be acceptable, or is there a different way to send a parameter by reference so that it is accessible in the calling function and modified value should be reflected ? Because this way doesn't seem right to me. Any help would be appreciated. Thanks.
I have one function :
jQuery('#'+'linkhere').click(getHashPageClick);
and in the function I am retreving window.location.hash value :
var getHashPageClick = function(evt) {
var hashParam = window.location.hash.substring(1);
}
But now I want to send a parameter to getHashPageClick(), say value , append hashParam value to it and access it in my calling function ? So something like this, maybe? :
jQuery('#'+'linkhere').click(getHashPageClick(value);
alert("value");
and in the called function modify value of value :
var getHashPageClick = function(evt,value) {
var hashParam = window.location.hash.substring(1);
valuevalue+hashParam;
return value;
}
Would that be acceptable, or is there a different way to send a parameter by reference so that it is accessible in the calling function and modified value should be reflected ? Because this way doesn't seem right to me. Any help would be appreciated. Thanks.
Share Improve this question asked Mar 24, 2011 at 3:02 TopCoderTopCoder 4,29619 gold badges56 silver badges65 bronze badges2 Answers
Reset to default 3You can't really pass a value by reference in JavaScript but you can pass something that is already a reference. For example, you could use an object literal to simulate a pointer:
function getHashPageClick(p) {
p.value = p.value + window.location.hash.substring(1);
}
var p = { value: 'some value' };
getHashPageClick(p);
Here's how I would go about setting up the functionality you're looking for:
jQuery(document).ready(function(){
var value = 'some value';
jQuery('#linkhere').click(function(){
value = getHashPageClick(value);
alert('the value is now: '+value);
});
});
var getHashPageClick(value) {
var hashParam = window.location.hash.substring(1);
value = value+hashParam;
return value;
}
Or, if you don't want to bump heads with just the one 'value' variable, create two variables to work in tandem:
jQuery(document).ready(function(){
var value = 'some value';
jQuery('#linkhere').click(function(){
hashValue = getHashPageClick(value);
alert('the hash value is now: '+hashValue);
});
});
var getHashPageClick(value) {
var hashParam = window.location.hash.substring(1);
var hashValue = value+hashParam;
return hashValue;
}
Hope this helps.