/    Sign up×
Community /Pin to ProfileBookmark

append variable to URL on page load

I am wanting to add a variable and its value to the end of the URL of the loaded page. This variable is coming from a selectedIndex value on the page.

Here is my script:

function appendURL()
{

var scopeopt = document.searchtool.searchscope.options[document.searchtool.searchscope.selectedIndex].value;
new_url = window.location.href + “/searchscope=” + scopeopt;
window.location = new_url;

}

I would ideally like this to happen when the page loads, but only once obvisously.

Am I on the right track or is there a better way?

Thanks

Stu

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@toicontienApr 02.2008 — If you append it to the URL portion of the web address, then you cause the page to reload. Instead you might try adding the variable to the anchor of the URL, the part of the web address following the "#" sign.
Copy linkTweet thisAlerts:
@WitchazelApr 03.2008 — new_url = window.location.href + "[B]?[/B]searchscope=" + scopeopt;

dont forget the ka (?)

and you won't need that slash (/)
Copy linkTweet thisAlerts:
@sureauthorApr 07.2008 — looks like the anchor idea was the winner! Thanks!

code is now:

unction appendURL()

{


var scopeopt = document.searchtool.searchscope.options[document.searchtool.searchscope.selectedIndex].value;

new_url = window.location.href + "#searchscope=" + scopeopt;

window.location = new_url;

}
×

Success!

Help @sure 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.18,
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,
)...