Bit of a javascript newbie so not sure if this question is easy or impossible.
Am working on a site with different testing environments and have made a greasemonkey script to make a set of buttons to switch from one to another:
if (/^(.*?)\www.mysite.com(.*)$/.test(document.location.href)) {
document.location.href = RegExp.$1+"iww1.mysite.com"+RegExp.$2
}
This has been working except for some URLs have a search string ID that also needs changing to a different number too.
&storeId=15162
I feel like I’ve exhausted my limited knowledge, by adding another if function within the {} to adding various replace functions, all to no avail.
If this makes sense and anyone can help it would be much appreciated.
Cheers
Your first problem is that the
locationobject is magical: assigning to it, or to any of its properties, causes the browser to navigate to the assigned URL. Since this will typically stop your script, you won’t be able to do it twice. Instead, you should store the intermediate URL in some other variable, and only assign it back tolocation.hrefonce you’re ready to move to the new page.Also, you didn’t specify how the new “search string ID” should be calculated, so I just assumed that you have some function, named e.g.
getNewStoreID(), that will return the new ID when you give it the old one. Given such a function, this code should do it: