Jump to content

String manipulation in JQuery


matfish

Recommended Posts

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

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?

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

  • 3 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.