in Education by
I'm trying to figure out a solution to allow an website to know what URL the user is on through an iframe. Website 1: http://website.website.com (Remote Website, can only add javascript & html to the webpage) Website 2: https://example.com (Fully Editable, php, html, js.. etc) Current Code: (Of Website 2 (Example.com) <!DOCTYPE html> Website.com jq = jQuery.noConflict(); jq(document).ready(function() { var currentFramePath = ''; var iframe = ''; var urlFrame = getUrlParameter('currentFrame'); if(urlFrame != null && urlFrame != ''){ console.log("Frame not found"); jq('#iFrameContainer').html(iframe.replace('{src}', urlFrame)); currentFramePath = urlFrame; } jq('#iFrameContainer').click(function(){ console.log("Clicked in frame"); currentFramePath = jq(this).attr('href'); console.log(currentFramePath); }); setInterval(function(){ window.location = window.location.href.split('?')[0] + '?currentFrame=' + currentFramePath; console.log("Update Query"); }, 5000); }); function getUrlParameter(sParam) { var sPageURL = decodeURIComponent(window.location.search.substring(1)), sURLVariables = sPageURL.split('&'), sParameterName, i; console.log("Get Query"); for (i = 0; i < sURLVariables.length; i++) { sParameterName = sURLVariables[i].split('='); if (sParameterName[0] === sParam) { return sParameterName[1] === undefined ? true : sParameterName[1]; } } };
Problem If I refresh the page (iframe) on example.com it refreshes and forgets the page that the user is/was on... As you can see I have attempted to get it working by detecting their page through an iFrame however this is impossible due to it being on a different domain. Solution? I'm looking for some sort of solution to do something like described below, bare in mind there could be a better solution. I want the website website.website.com to get the current path / url of the page the user is on (which is being viewed through an iframe) and for it to send this path/url through to example.com then example.com would update the session / temporary cookie / temporary local storage / variable... etc which would then mean it would adjust the query string to point itself to the correct URL for when the user refreshes their page resulting in the refresh correctly remembering the page they were on. Attempt I tried to use the postMessage function by putting the follow code on their respective sites: Website 1 Extra Code setInterval(function() { parent.postMessage(window.location.pathname, "https://website.com"); },1000); Website 2 Extra Code: var eventMethod = window.addEventListener ? "addEventListener" : "attachEvent"; var eventer = window[eventMethod]; var messageEvent = eventMethod == "attachEvent" ? "onmessage" : "message"; eventer(messageEvent, function(e) { console.log('Parent Message: ', e.data); }, false); However nothing happens, no console messages or errors... just nothing. I've even tried copying the likes of https://blog.teamtreehouse.com/cross-domain-messaging-with-postmessage but nothing in that helped :( Any ideas what I am doing wrong and a way to resolve it to achieve this? Thanks Edits I've tried the following js inside http://website.website.com but it didn't work: localStorage.setItem('CurrentURLChecker', window.location.href) if (localStorage.getItem('CurrentURLChecker')) { if (window.parent.location.href == "https://website.com/" ) { console.log("URL FOUND"); } } Uncaught DOMException: Blocked a frame with origin "http://website.website.com" from accessing a cross-origin frame at http://website.website.com/:251:44 EDIT - An example Website 1 = "http://stackoverflow.serviceprovider.com" Website 2 = "https://stackoverflow.com" Website 2 contains an iframe which shows the exactly what Website 1 shows. I am never going to visit Website 1, all clicks are done on Website 2 If I was to click on a link inside the iframe and it was to navigate to: http://stackoverflow.serviceprovider.com/this-new-page/ then Website 1 should be able to detect this and store the iframes location and remember it. Now if I refresh my browser instead of the iframe loading http://stackoverflow.serviceprovider.com it would instead load the page they actually refreshed which is http://stackoverflow.serviceprovider.com/this-new-page/ The tab/window URL will always stay on https://stackoverflow.com/ but it would be a necessity to append a query string so the links can be made sharable. It's that simple. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
For security reasons, you can only get the url for as long as the contents of the iframe, and the referencing javascript, are served from the same domain. If the two domains are mismatched, you'll run into cross site reference scripting security restrictions.

Related questions

0 votes
    Which of these methods is used to know host of an URL? (a) host() (b) getHost() (c) GetHost( ... java programming questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    Which of these methods is used to know the full URL of an URL object? (a) fullHost() (b) ... java programming questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    Which of these methods is used to know when was the URL last modified? (a) LastModified() (b) ... programming questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    Which of these methods is used to know the type of content used in the URL? (a) ContentType() (b ... programming questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    I'm wanting to make a bookmarklet that would change a webpage from http://user.tumblr.com/post/123456789 ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    i have a website that contain an iframe. www.vendeposto.com.br and i want to hide an element Im trying to do this ... the vendeposto.com.br site is this: Postos de gasolina a venda...
asked May 1, 2022 in Education by JackTerrance
0 votes
    i have a website that contain an iframe. www.vendeposto.com.br and i want to hide an element Im trying to do this ... the vendeposto.com.br site is this: Postos de gasolina a venda...
asked Apr 29, 2022 in Education by JackTerrance
0 votes
    i have a website that contain an iframe. www.vendeposto.com.br and i want to hide an element Im trying to do this ... the vendeposto.com.br site is this: Postos de gasolina a venda...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I'm attempting to automate a payment system, where the "Pay with PayPal" button is within an iFrame. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    I have built an Android App using Phonegap/HTML5/JQuery mobile. I had the option of parsing XML data ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 21, 2022 in Education by JackTerrance
0 votes
0 votes
    HTTP defines two ways in which values entered by a user at the browser can be sent to the Web ... Answers, Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    External clients communicate with Microservices using ___________. A. Messaging B. API C. API Gateway D. API Proxy...
asked Jan 9, 2023 in Technology by JackTerrance
0 votes
    How does applet and servlet communicate? (a) HTTP (b) HTTPS (c) FTP (d) HTTP Tunneling I have been ... Technologies & Servlet of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    What are the words we should use when we communicate verbally Select the correct answer from above options...
asked Dec 31, 2021 in Education by JackTerrance
...