Home >
Technical & Creative Skills >
Programming, Servers & Scripts
Get URL from iframe? (5)
04-28-2021 09:09 PM
#1
choose_a_username (Member)
Get URL from iframe?
I have a circumstance where I need to to get my click ID before I send the user to a different site.
My plan is to open my click in an iframe, wait for it to redirect, and then grab the parameters that are in the URL.
Code:
var x = document.createElement("IFRAME"); x.setAttribute("src", "http://mysrc.com/redirect1");
document.body.appendChild(x);
The problem is, it appears that I'm unable to get the URL after it redirects. All I can get is x.src, which just returns "http://mysrc.com/redirect1", which is what my link is before it redirects.
Is there a solution to this?
04-28-2021 09:54 PM
#2
jeremie (Moderator)
x.src is not updated when the iframe content changes, only when you refresh the main page.
What you need is:
Code:
x.addEventListener("load", function () {
console.log(this.contentWindow.location.href)
});
BUT
Most of the time, it will not work if the domain loaded in the IFRAME is not the same as the domain of the main page. It only works when the domain in the IFRAME specifically accepts cross-origin requests, which by default is not the case nowadays. But if you have access to the domain you are loading, you can configure it to send a header allowing cross-origin requests.

Originally Posted by
choose_a_username
Is there a solution to this?
Probably not the way you are trying to do it.
Tell us a bit more about your setup if you want a more detailed solution. What tracker are you using? Several trackers now ship a JS pixel that allows you to communicate and get tracker data from the page.
04-29-2021 01:22 PM
#3
choose_a_username (Member)
I have access to the domain I'm loading, so this will work for me, thanks!

Originally Posted by
jeremie
x.src is not updated when the iframe content changes, only when you refresh the main page.
What you need is:
Code:
x.addEventListener("load", function () {
console.log(this.contentWindow.location.href)
});
BUT
Most of the time, it will not work if the domain loaded in the IFRAME is not the same as the domain of the main page. It only works when the domain in the IFRAME specifically accepts cross-origin requests, which by default is not the case nowadays. But if you have access to the domain you are loading, you can configure it to send a header allowing cross-origin requests.
Probably not the way you are trying to do it.
Tell us a bit more about your setup if you want a more detailed solution. What tracker are you using? Several trackers now ship a JS pixel that allows you to communicate and get tracker data from the page.
04-29-2021 06:29 PM
#4
jeremie (Moderator)
Great.
Here are some info about the headers to send:
https://developer.mozilla.org/fr/docs/Web/HTTP/CORS
04-30-2021 11:43 AM
#5
twinaxe (Senior Moderator)
Would be helpful to understand better what you want to achieve.
When it´s just a normal campaign flow you don´t need any of these things.
The infos are stored in the cookies and database.
Just add the click ID in the tracker to the offer URL like https://fantasticoffer.com?s2={clickid}.
When you send a user through the campaign link to mydomain.com/index.html then the user gets a unique ID = click ID in your tracker.
Then you can send the user to wherever you want.
When the user then clicks your click URL he gets redirected through the tracker again and based on the cookie the click ID will be automatically attached to the offer URL.
This works as long as the cookie is valid.
Home >
Technical & Creative Skills >
Programming, Servers & Scripts