matfish Posted June 3, 2010 Share Posted June 3, 2010 Hi there, JS newbie and trying to use JQuery, is there a way to do the following (having to write in pseudo code so you know what I'm trying to do...) (oh, and I'm dealing with iFrame issues - something I have to do for this project) top.location.href="http://www.domain.com/directory/?foo=1234"; if(top.location.href contains "foo=1234variable"){ openPopup('1234variable'); } So basically, use JQuery to get the full URL and IF there is a foo variable then open the ID 1234 in my popup? Many thanks Quote Link to comment Share on other sites More sharing options...
matfish Posted June 3, 2010 Author Share Posted June 3, 2010 Or maybe change the src of the iframe and load index.php?foo=1234 which will load the variable within the frame? Quote Link to comment Share on other sites More sharing options...
matfish Posted June 3, 2010 Author Share Posted June 3, 2010 You know what... document.getElementById('iFrame').src = 'http://www.domain.com/index.php?foo=1234'; Did just what I wanted. Quote Link to comment Share on other sites More sharing options...
matfish Posted June 3, 2010 Author Share Posted June 3, 2010 I still need to get the foo=1234 using Javascript/JQuery from the parent URL so I can pass it to the iFrame if anyone could help? Quote Link to comment Share on other sites More sharing options...
matfish Posted June 4, 2010 Author Share Posted June 4, 2010 I currently have the code: <script> function strstr (haystack, needle, bool) { var pos = 0; haystack += ''; pos = haystack.indexOf( needle ); if (pos == -1) { return false; } else{ if (bool){ return haystack.substr( 0, pos ); } else{ amp = haystack.indexOf('&'); if(amp>pos){ return haystack.slice( pos, amp ); }else{ return haystack.slice( pos ); }//if(amp>1){ }//if (bool){ }//if (pos == -1) { }//function strstr var string = strstr("thisisatest.com/index.php?variable1=1234&variable2=4321&variable3=000", "variable2="); alert(string); </script> If i change the variable2= in the strstr function to variable1= then it works, but not if I use variable 2 or variable 3. I guess this is something to do with the & being in seveal places? Quote Link to comment Share on other sites More sharing options...
matfish Posted June 4, 2010 Author Share Posted June 4, 2010 I've altered to suit what I needed and seems to work now: <script> function strstr (haystack, needle, bool) { var pos1 = 0; haystack += ''; pos1 = haystack.indexOf( needle ); if (pos1 == -1) { return false; } else{ if (bool){ return haystack.substr(0, pos1); } else{ amp1 = haystack.indexOf('&'); if(amp1>pos1){ return haystack.slice(pos1,amp1); }else{ bString = haystack.slice( pos1 ); pos2 = bString.indexOf( needle ); amp2 = bString.indexOf('&'); return bString.slice( pos2, amp2); }//if(amp>1){ }//if (bool){ }//if (pos == -1) { }//function strstr var string = strstr("thisisatest.com/index.php?variable1=1234&variable2=4321&variable3=000", "variable1="); alert(string); </script> If anyone does know a simpler way, I'd be interested. Many thanks Quote Link to comment Share on other sites More sharing options...
plutomed Posted June 5, 2010 Share Posted June 5, 2010 You could use regex. var var1=new RegExp("foo=12345"); var link = "http://www.domain.com/directory/?foo=1234" if(var1.test(link) == true) { //Do whatever } Quote Link to comment Share on other sites More sharing options...
matfish Posted June 24, 2010 Author Share Posted June 24, 2010 hmmm, Is there any way of getting the query string window.location.search.substring(1) for example from within the iFrame? I'm trying to do all this from within the iFrame as the iFrame will be hosted elsewhere. Many thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.