3

If my url is http://link-ads.blogspot.com/?url=http://stackoverflow.com/ The code below extracts http://stackoverflow.com/

<script type='text/javascript'>
$(document).ready(main);

function main()
{
    $('#download, #imggg').attr('href', getIframeUrl() );
    registerEvents();
    resizeIframe();
}

function getIframeUrl()
{
    var url = window.location.href;
    var iframe_url = 'http://link-ads.blogspot.com/';
    var param_start = url.indexOf("url=");
    if( param_start != -1 ) 
        iframe_url = url.substr(param_start+4,url.length-param_start-4);
    if( iframe_url.indexOf("http://") == -1) 
        iframe_url = "http://" + iframe_url;

    return iframe_url;
}
  </script>

my problem is if the url is http://link-ads.blogspot.com/?url=http://stackoverflow.com/&adclient=12345678kkk1112 the code will extract the url as http://stackoverflow.com/&adclient=12345678kkk1112 when I want the url before &adclient= meaning I only want it to extract http://stackoverflow.com/

Yusaf Khaliq
  • 3,333
  • 11
  • 42
  • 82
  • You could use the [same code you received](http://stackoverflow.com/questions/7910549/how-to-textract-client-from-url/7910577#7910577) 30 minutes ago. – alex Oct 27 '11 at 01:13
  • I don't know if I want to post it twice, but you can delete your question if you feel it is a duplicate. – alex Oct 27 '11 at 01:19
  • `if (link.substr(0, 7) !== 'http://') { link = 'http://' + link; }`. – alex Oct 27 '11 at 01:27
  • how do i implement it in this ? `` – Yusaf Khaliq Oct 27 '11 at 01:30

1 Answers1

1

I'd suggest using a regular expression:

var url = "http://link-ads.blogspot.com/?url=http://stackoverflow.com/&adclient=12345678kkk1112"
url.match(/url=([^&]+)/)[1]
Alex Peattie
  • 26,633
  • 5
  • 50
  • 52
  • would you know how to implement this `if (link.substr(0, 7) !== 'http://') { link = 'http://' + link; }` into this `` – Yusaf Khaliq Oct 27 '11 at 02:11