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

Link to comment
Share on other sites

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
Share on other sites

  • 3 weeks later...

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.