tengo dos dominios Uno para vender productos que es https://sellproducts.com y el otro para documentación de productos que es https://docs.product.wiki
En https://sellproducts.com tengo una página llamada docs ( https://sellproducts.com/docs ) que usé iframe para llamar o mostrar contenidos de https://docs.product.wiki
<iframe id="docs" src="https://docs.product.wiki/" frameborder="0"> </iframe>El https://docs.product.wiki tiene muchas páginas de ejemplo,
https://docs.product.wiki/intro.html
https://docs.product.wiki/about.hml
Quiero usar javascript o jquery para obtener la URL actual de iframe y mostrarla en el navegador como " https://sellproducts.com/docs?page=intro" , cuando se hace clic en una página o se vuelve a cargar.
Si puedes poner algunos js en ambos lados, es posible.
En orden, ahí está la lógica que necesitas:
Lo siguiente podría ser un buen comienzo:
En su https://sellproducts.com/docs ponga este código:
window.onload = function(e) { const docsUrl = 'https://docs.product.wiki/'; const queryString = window.location.search; //Parse URL to get params like ?page= let iframe; if(document.querySelector('iframe').length) //If iframe exit use it iframe = document.querySelector('iframe'); else iframe = document.createElement('iframe'); //Create iframe element iframe.src = docsUrl; //Set default URL iframeframeBorder = 0; //Set frameborder 0 (optional) if (queryString !== '') { const urlParams = new URLSearchParams(queryString); //Convert to URLSearchParams, easy to manipulate after const page = urlParams.get('page'); //Get the desired params value here "page" iframe.src = docsUrl+page + '.html'; //Set iframe src example if ?page=intro so url is https://docs.product.wiki/intro.html } if(!document.querySelector('iframe').length) document.body.appendChild(iframe);//Append iframe to DOM }Y el lado https://docs.product.wiki coloca este código en tu plantilla global (debe estar en todas las páginas):
let links = document.querySelectorAll('a'); //Get all link tag <a> links.forEach(function(link) { //Loop on each <a> link.addEventListener('click', function(e) { //Add click event listener let target = e.target.href; //Get href value of clicked link let page = target.split("/").pop(); //Split it to get the page (eg: page.html) page = page.replace(/\.[^/.]+$/, ""); //Remove .html so we get page let currentHref = window.top.location.href; //Get the current windows location //console.log(window.location.hostname+'/docs?page='+page); window.top.location.href = 'https://sellproducts.com/docs?page='+page; //Set the current window (not the frame) location e.preventDefault(); }); });Comentarios apreciados :)