/    Sign up×
Community /Pin to ProfileBookmark

How Do I Modify URL by Adding Values Based on Conditions?

Hello Everyone,

I am new to this forum and also new to JavaScript programming. Please forgive my ignorance upfront. (I truly mean it…I am totally green to javascript)

I am trying to create bookmarklets for both IE & FireFox that allow me to create certain test urls for a staging site.

What I need to do is be able to have the script add to the end of the live url string ‘?test=true’.

I have site that if I add the paratmeters “?test=true” to the end of the page, (i.e. [url]www.mysite.com?test=true[/url]) it will show me a test version of that page that displays content not accessible if I just go to the regular [url]www.mysite.com[/url] URL. Also, this can be done on ANY page of the site.

So the first thing that this bookmarklet needs to do is take the URL of the page that I am browsing right now and add to it at the very end “?test=true”

Of course, this poses a problem in that if the URL that I am browsing right now already has the hook (?) in it, I cannot add another one and therefore need the bookmarklet to detect if any (?) is in the URL and if so place “&test=true” instead of “?test=true” at the end of the URL.

Here is the code that I am working with:

For IE:

[CODE]<html>
<head>
<script language=”JavaScript” TYPE=”text/javascript” >
function runme() {
var proxyHost, fullUrl, endIdx, idx, prefix, url, hostName, flippedHost;
proxyHost = ‘mysite.com’;
fullUrl = prompt(‘URL to Test:’,”);
endIdx = fullUrl.indexOf(‘?’);
if (endIdx < 0) endIdx = fullUrl.length;
idx = fullUrl.indexOf(‘//’);
if (idx==0 || idx==5 || idx==6) {
idx += 2;
}
else {
idx = 0;
}
if (fullUrl.indexOf(‘https:’) >= 0) {
prefix = ‘https://’;
}
else {
prefix = ‘http://’;
}
url = fullUrl.substring(idx,fullUrl.length);
idx = url.indexOf(‘/’);
if (idx == -1) idx = url.length;
hostName = url.substring(0,idx);
flippedHost = ”;
for (i = hostName.length-1; i >= 0; i–) flippedHost += hostName.charAt(i) == ‘.’ ? ‘/’ : hostName.charAt(i);
document.write(‘<a href = “‘ + prefix + proxyHost + ‘/’ + flippedHost + ‘/u1=$(vid)/%5e’ + url.substring(idx,url.length)+ ‘”>’+prefix + proxyHost + ‘/’ + flippedHost + ‘/u1=$(vid)/%5e’ + url.substring(idx,url.length)+ ‘</a><br>’);
document.close();
}
</script>
</head>
<body onload = “runme()”>

</body>
</html>

[/CODE]

For FireFox

[CODE]javascript: var proxyHost, fullUrl, endIdx, idx, prefix, url, hostName, flippedHost; proxyHost = ‘mysite.com’; fullUrl = prompt(‘URL to proxy:’,”); endIdx = fullUrl.indexOf(‘?’); if (endIdx < 0) endIdx = fullUrl.length; idx = fullUrl.indexOf(‘//’); if (idx==0 || idx==5 || idx==6) { idx += 2; } else { idx = 0; } if (fullUrl.indexOf(‘https:’) >= 0) {prefix = ‘https://’; }else {prefix = ‘http://’; } url = fullUrl.substring(idx,fullUrl.length); idx = url.indexOf(‘/’); if (idx == -1) idx = url.length; hostName = url.substring(0,idx); flippedHost = ”; for (i = hostName.length-1; i >= 0; i–) flippedHost += hostName.charAt(i) == ‘.’ ? ‘/’ : hostName.charAt(i); document.write(‘<a href = “‘ + prefix + proxyHost + ‘/’ + flippedHost + ‘/u1=$(vid)/%255e’ + url.substring(idx,url.length)+ ‘”>’+prefix + proxyHost + ‘/’ + flippedHost + ‘/u1=$(vid)/%255e’ + url.substring(idx,url.length)+ ‘</a><br>’);document.close();[/CODE]

Thank you all very much for your help! ?

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @RuddyO 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.28,
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,
)...