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 Link to comment https://forums.phpfreaks.com/topic/203764-string-manipulation-in-jquery/ 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? Link to comment https://forums.phpfreaks.com/topic/203764-string-manipulation-in-jquery/#findComment-1067239 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. Link to comment https://forums.phpfreaks.com/topic/203764-string-manipulation-in-jquery/#findComment-1067243 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? Link to comment https://forums.phpfreaks.com/topic/203764-string-manipulation-in-jquery/#findComment-1067245 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? Link to comment https://forums.phpfreaks.com/topic/203764-string-manipulation-in-jquery/#findComment-1067598 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 Link to comment https://forums.phpfreaks.com/topic/203764-string-manipulation-in-jquery/#findComment-1067608 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 } Link to comment https://forums.phpfreaks.com/topic/203764-string-manipulation-in-jquery/#findComment-1068380 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 Link to comment https://forums.phpfreaks.com/topic/203764-string-manipulation-in-jquery/#findComment-1076534 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.