/    Sign up×
Community /Pin to ProfileBookmark

Multiple attributes into query string

Hi everyone!

I have the following function:

[CODE]function goToPageWithAttributes(page,attr,value){
//alert(“goToPageWithAttributes :” + page + ” attr : ” + attr + ” value : ” + value );

var container = MyOpenSpace.MySpaceContainer.get();
var qryString = container.getQueryString();

var qryString = replaceQueryString(qryString,attr,value);

//alert(qryString)

window.location = page + “?” + qryString
}[/CODE]

I am very rusty with my Javascript and am wondering how I would re-write this (if I even need to) to support multiple attributes and values. In other words, a=1, b=2, c=3, d=compose, e=whatever.

If anyone has any insight as to how I could accomplish this, I will be in your debt.

Cheers!

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991Jun 03.2009 — Yes, look into the arguments array.<i>
</i>function foo() {
alert(arguments);
}
foo(6,7,8);
Copy linkTweet thisAlerts:
@astupidnameJun 03.2009 — You could either send the attr and value as individual strings specifying single items or you could send attr and value as arrays of items:

[CODE]<script type="text/javascript">

function goToPageWithAttributes(page,attr,value){
var append, i, L, str = '';
if (attr instanceof Array && value instanceof Array) {
L = Math.min(attr.length, value.length); //in case an array is different length than other
for (i = 0; i < L; i++) {
append = attr[i] + '=' + value[i];
str += (str.length) ? '&' + append : append;
}
} else {
str += attr + '=' + value;
}
if (str.length) {
str = page + '?' + str;
alert(str)
//window.location.href = str;
}
}

goToPageWithAttributes('http://www.example.com', ['foo', 'footwo'], ['bar', 'baranamous']);
goToPageWithAttributes('http://www.example.com', 'sumnum', 22);

</script>[/CODE]
×

Success!

Help @mikeurturn 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.26,
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,
)...