Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Aug 2007
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    How do I Add Variables to URL Based on Conditions?

    Hello Everyone,

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

    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:

    Code:
    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?

  • #2
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    That code effectively contains such a test already:
    Quote Originally Posted by RuddyO View Post
    endIdx = fullUrl.indexOf('?');
    So what's the problem?

  • Users who have thanked Arty Effem for this post:

    RuddyO (08-30-2007)

  • #3
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    detect that "?" and if it finds it tell it to use "&test=true" instead
    You could use the replace method:
    Code:
    var q = '?test=true';
    if (/\?/.test(fullUrl)) {
     fullUrl = fullUrl.replace(/\?(.|\w)*$/, q);
    } else {
     fullUrl += q;
    }

  • Users who have thanked rwedge for this post:

    RuddyO (08-30-2007)

  • #4
    New to the CF scene
    Join Date
    Aug 2007
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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!

    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?

    Code:
    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

  • #5
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    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.

  • Users who have thanked rwedge for this post:

    RuddyO (08-31-2007)

  • #6
    New to the CF scene
    Join Date
    Aug 2007
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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:

    Code:
    <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?

  • #7
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    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:
    Code:
    <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>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •