...

View Full Version : How do I Add Variables to URL Based on Conditions?



RuddyO
08-23-2007, 08:52 PM
Hello Everyone,

I am new to this forum and also new to JavaScript programming. Please forgive my ignorance upfront. :o

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'.

However since some of the urls generated by the system already have "?" in it some where, I have to have the script detect that "?" and if it finds it tell it to use "&test=true" instead.

Here is the code that I am working with:


javascript: var proxyHost, fullUrl, endIdx, idx, prefix, url, hostName, flippedHost; proxyHost = 'sp.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();



Anybody have any ideas how this can be accomplished?

Arty Effem
08-24-2007, 02:10 AM
That code effectively contains such a test already:

endIdx = fullUrl.indexOf('?');
So what's the problem?

rwedge
08-24-2007, 03:23 AM
detect that "?" and if it finds it tell it to use "&test=true" instead
You could use the replace method:
var q = '?test=true';
if (/\?/.test(fullUrl)) {
fullUrl = fullUrl.replace(/\?(.|\w)*$/, q);
} else {
fullUrl += q;
}

RuddyO
08-30-2007, 08:59 PM
Thank so much Arty Effem & rwedge!

I should have told the forum to email me when people place a response to my posts! I would've have come back to the party days ago! :o

However, I think I need to be more clear & specific:

I have site that if I add the paratmeters "?test=true" to the end of the page, (i.e. www.mysite.com?test=true) it will show me a test version of that page that displays content not accessible if I just go to the regular www.mysite.com URL. 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.

rwedge,

Looks like you have provided a great solution to 1/2 of my problem.

My only question is, where exactly in the current string of code do I place this snippet into?

(Remember guys, I am totally green on the JavaScript side of things)

Would this work?



javascript:
var proxyHost, fullUrl, endIdx, idx, prefix, url, hostName, flippedHost; proxyHost = 'sp.mysite.com';
fullUrl = prompt('URL to Test:','');
endIdx = fullUrl.indexOf('?');
var q = '?test=true';
if (/\?/.test(fullUrl)) {
fullUrl = fullUrl.replace(/\?(.|\w)*$/, q);
} else {
fullUrl += q;
}
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();
// JavaScript Document

rwedge
08-31-2007, 01:39 AM
The location will work. The requirement, of course, is that it must appear after the fullUrl prompt.

You would not need the document.close() as the script will be writing to the same page the prompt is on.
Loading will hang otherwise.

RuddyO
08-31-2007, 07:02 PM
Rwedge,

I tried it but for some reason it doesn't seem to work right.

Here's the code as I have it on the page:


<html>
<head>
<script language="JavaScript" TYPE="text/javascript" >
function runme() {
var proxyHost, fullUrl, endIdx, idx, prefix, url, hostName, flippedHost;
proxyHost = 'sp.mysite.com';
fullUrl = prompt('URL to Test:','');
endIdx = fullUrl.indexOf('?');
var q = '?test=true';
if (/\?/.test(fullUrl)) {
fullUrl = fullUrl.replace(/\?(.|\w)*$/, q);
} else {
fullUrl += q;
}
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>');
</script>
</head>
<body onload = "runme()">

</body>
</html>


Not sure why it's not working properly...anything that you see not right in it?

rwedge
08-31-2007, 10:44 PM
You should enclose your loop statements with '{}' and your function is missing the closing '}'
Using document.write, after page load will open a new window.

This should work:
<html>
<head>
<script type="text/javascript">
function runme() {
var proxyHost, fullUrl, endIdx, idx, prefix, url, hostName, flippedHost, q = '?test=true', theUrls = '';
proxyHost = 'sp.mysite.com';
fullUrl = prompt('URL to Test:','');
endIdx = fullUrl.indexOf('?');
if (/\?/.test(fullUrl)) {
fullUrl = fullUrl.replace(/\?(.|\w)*$/, q);
} else {
fullUrl += q;
}
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);
theUrls += '<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>';
}
var myDiv = document.createElement('div');
myDiv.setAttribute('id','results');
document.body.appendChild(myDiv);
document.getElementById('results').innerHTML = theUrls;
}
window.onload = runme;
</script>
</head>
<body>


</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum