0

So I wrote some cookies, but I need to append the information to the end of the src in an iframe. The problem is I can't figure out how to select the iframe because it doesn't have an id.

The iframe looks like this:

<iframe src="www.mysite.com";>

I need it to look like:

<iframe src="www.mysite.com&cookie";>

However I can not use an id to select the iframe because I am not the one creating the iframe and if there are iframes on other pages they need to be selected too and updated in the same way. So the function needs to be universal. If you want to see my code for the cookies I can also show you that.

If this is too vague let me know and I'll update it.

Here is a fiddle of what I think I am trying to do.

http://jsfiddle.net/Inzblaze/2h6ke9vg/3/

  • Do any of it's parents have an ID? Can you show a few more layers of your DOM tree? Maybe the parent has an ID and you can use that to directly access this iFrame – Adjit Oct 19 '15 at 17:32
  • one of the div's above it has a class="container" but it is quite a few div's up. –  Oct 19 '15 at 17:51
  • is there anything unique about the selector? It doesn't matter if it is a few divs up, you can be super specific with your CSS selector (ie. `body .someClass #someID .anotherClass .container > div > div >iFrame`) the `>` refers to a direct descendant – Adjit Oct 19 '15 at 18:51
  • I just gave a fiddle a go.... Do you think this would work? http://jsfiddle.net/Inzblaze/2h6ke9vg/3/ –  Oct 19 '15 at 19:13
  • Not really sure. Your `search lead` functions aren't doing anything. If you want to retrieve the cookie values take a look at this question for some help http://stackoverflow.com/questions/10730362/get-cookie-by-name – Adjit Oct 19 '15 at 19:45

2 Answers2

0

You need to append something to EVERY iframe src?

var iframes = document.querySelectorAll('iframe');
for (var i = 0; i < iframes.length; i++) {
    iframes[i].src += 'something';
}
bcr
  • 3,791
  • 18
  • 28
  • Every instance of the iframe. So there is only one iframe per page but multiple pages. Every time they load a new page the iframe needs to be appended. Although that may have helped me regardless... I will try out some code and get back to you. –  Oct 19 '15 at 17:43
  • What i tried didn't work... Hmmm... I am a fairly new programmer just trying to learn the basics. If I show you what the cookie i built looks like, would that help? And i suppose that code could work but i still don't understand how i'd append the cookie as a string to the end of src. –  Oct 19 '15 at 18:05
0

Will the src attribute always be the same? You could select it by:

var frame = $("iframe[src='www.mysite.com']"); 

Edit:

Here is how you can get and append the cookie:

Document.cookie will return the entire cookie as a string, and I am guessing you probably only want a certain value from that cookie. So you could do something like:

// This is just declaring a function that you can use to get cookie values. 
function getCookie(cookieName) {
    // If you open up your console and type in document.cookie, you will see
    // that all cookie values are inside of this same string. So this
    // function will allow you to extract the value you are looking for

    // We start by creating a new Regular Expression object, that will
    // be used to match a certain pattern of text.
    var re = new RegExp('[; ]'+cookieName+'=([^\\s;]*)');
    // This line actually uses our pattern, and returns the match from 
    // the cookie. 
    var sMatch = (' '+document.cookie).match(re);

    // if we have a cookie name, and our regular expression found a match
    // in the cookie, then return the value that was found from the match
    if (cookieName && sMatch) {
        return unescape(sMatch[1]);
    }

    // if we dont find a matching value, just return an empty string
    return '';
}

Which allows you to do something like:

// Now we are actually CALLING our function, and pass in whatever we named
// our cookie value. The variable cookieValue will then hold this value. 
var cookieValue = getCookie("cookieName"); 

// Here we are using jQuery to find the iframe element we want to manipulate
// and save reference to it in a variable called 'frame'
var frame = $("iframe[src='www.mysite.com']"); 

// From this element, we grab it's current src value
var currentSrc = frame.attr("src"); 
// here, we SET the frames src value, by setting it back to it's current src
// and tack on the cookie value
frame.attr("src", currentSrc + "&" + cookieValue); 
KDot
  • 496
  • 2
  • 7
  • Yes the src will always be the same. after I select it with that, and append what i need, would I have to reload the iframe? –  Oct 19 '15 at 17:44
  • No, you shouldn't have to. It should automatically reload. – KDot Oct 19 '15 at 17:59
  • Sorry, I am just really new to programming... How would I append the cookie as a string to the end of the src? This is all for a test site im working on, im just trying to understand the basics. –  Oct 19 '15 at 18:13
  • Did you see my edit to my answer above? I explained a method to get the cookie value and append it to the iframe src. – KDot Oct 19 '15 at 19:55
  • Is there any way you could take the time to explain to me what each line does? I'm sorry but like I said I am new and I do not completely understand that... –  Oct 19 '15 at 19:56
  • Sure thing. Before I get started, I want to point out that I changed the getCookie function, because the one I was using before was sort of inefficient. I will add some inline comments in my answer above. – KDot Oct 19 '15 at 20:16
  • Thank you so much! This actually helped out a lot. –  Oct 19 '15 at 21:27