Make a page reload and execute a script from the URL - javascript

This is what I've got so far:
var x = document.getElementsByName("treeframe")[0].contentDocument.getElementsByTagName("a");
for(var idx = 0; idx < x.length; idx++){
var link = x[idx].href;
if ( link.indexOf("STRING_TO_SEARCH") != -1){
alert("found!!!");
} else {
window.setTimeout("location.reload(true);",10000);
}
}
The thing is that, after the reload when it is not found, it does not re-excecute the script.
Note: the getElementsByName is needed because I need to search in a frame inside the page, but can not acces ONLY the iframe

So you're talking about a bookmarklet? Or do you have this thing in a Frameset? If its in a frameset, and you control the frameset then you should be able to attach an event listener to the element that you are reloading. Otherwise you can probably do some form of polling using either setTimeout or setInterval to check to see if the contents of the frame are accessible.

Related

Activate iframe and change its contents

I am losing my marbles over this issue:
There's an iframe that i try to programmatically manipulate, but somehow this is only possible after i go into the developer console and click a parent node.
This is the best i can come up with:
var iframes = document.getElementsByTagName('iframe');
for (var i = 0; i < iframes.length; i++) {
if (iframes[i].parentNode.id == 'cke_355_contents') {
iframes[i].contentWindow.document.open();
iframes[i].contentWindow.document.write('<html><body>Hello World</body></html>');
iframes[i].contentWindow.document.close();
}
}
Even if i try to list all iframes on the page:
document.querySelectorAll('iframe').forEach((iframe)=> {
console.log(iframe.id, iframe.className, iframe.src)
});
i just get the first 2 results. But, when i click a parentnode in the console, it will only list the iframe i actually need.
Is there some way to find the correct iframe,"activate" it and manipulate it?

get a dynamic id from an iframe in an iframe

I am actually doing a tricky task, I have to create pack of resource(which are pages on the website), to do so I use iframe to display the content of the pages. But I can have multiples Iframes in one Iframe.
And I want to pass some style on those iframe in iframe, so i have to target them.
I have a special node id for each pages that allow me to return only the body.
So my question is how do I get to target the id of my iframe in my iframe which I tried to do with that line var get_iframe_inside = search_inside.getElementsByTagName("iframe".id); to then modify it's style.
I know that I am not using the right way for this line, but I have been scratching my head all this morning and can't find a way.
function test(id){
var iframe = window.parent.document.getElementById(id); //select my first iframe
get_iframe_inside(id); //call my function to get the iframe in the iframe
function get_iframe_inside (id){
var search_inside = (iframe.contentDocument) ?iframe.contentDocument : iframe.contentWindow.document;
//My goal is then to modify some properties
var get_iframe_inside = search_inside.getElementsByTagName("iframe".id);
$(get_iframe_inside).css({'padding':'0px 50px', 'background-color':'#cecece'});
}
}
Well it was kind of trivial my code was nearly working i just didn't tought at how to get thoses ids.
i just had to get them by tag and after that to do an iteration with for.
var get_iframe_inside = search_inside.getElementsByTagName("iframe");
var i;
for (i = 0; i < get_iframe_inside.length; i++){
get_iframe_inside[i].style.padding='0px 50px';

Disable HTML auto-refresh?

A client's website has an auto-refresh feature which works great for desktop sites, but I'm making an iPhone app and need to disable it on the mobile version.
The code used is:
<META HTTP-EQUIV="REFRESH" CONTENT="30">
I would like to use javascript to disable it, if possible.
Thanks.
EDIT:
I DO NOT have access to the HTML file, and therefore can't modify it. I need to do this via code on the Objective-C side in Xcode.
I will introduce something simple
Add meta tag with ID id="meta-refresh" like this:
<meta http-equiv="refresh" content="2;http://new-url/" id="meta-refresh">
Just use these script..
var iOS = false,
p = navigator.platform;
if( p === 'iPad' || p === 'iPhone' || p === 'iPod' ){//remove which is not your target
iOS = true;
}
if(iOS){ // check if iOS then do the following
var mr = document.getElementById("meta-refresh");
mr.parentNode.removeChild(mr);
}
I believe this will work..
Also, the initiated request which JavaScript can not disable once loaded!!
For the same, the exact work around one can find in an Old Post answer given by user XP1
The above is using xmlhttp requrest (AJAX) to check before the document is loaded and remove the meta tag if the device is target device(iphone)
OR
one can use refresh dynamically id the device is not iPhone/iOS. that will remove the requirement of doing the dynamic check and requirement to avoid first refresh call. without using meta tag
var iOS = false,
p = navigator.platform;
if( p === 'iPad' || p === 'iPhone' || p === 'iPod' ){//remove which is not your target
iOS = true;
}
if(!iOS){
window.setTimeout(function(){window.location.href=window.location.href},30000); //30 Seconds
}
Assuming you are using a UIWebView to display the site, you could use [webView stringByEvaluatingJavaScriptFromString:#"..."];, where "..." is this javascript:
var metaTags = document.getElementsByTagName("META");
for(var i = 0; i < metaTags.length; i++) {
if (metaTags[i].getAttribute("HTTP-EQUIV").match(/^REFRESH$/i))
metaTags[i].parentNode.removeChild(metaTags[i]);
}
Condensed down to one line for convenience:
var metaTags = document.getElementsByTagName("META"); for(var i = 0; i < metaTags.length; i++) { if (metaTags[i].getAttribute("HTTP-EQUIV").match(/^REFRESH$/i)) { metaTags[i].parentNode.removeChild(metaTags[i]);}}
Edit: Well...after all that, turns out it's not possible to cancel an existing refresh request by simply removing the meta tag from the document. Once the parser sees the meta tag, it will refresh regardless of any javascript trickery you do. Unfortunately, the only way to overcome this is to modify the HTML page directly.
short answer: window.location.reload = () => {}
longer answer:
most browsers support some modification of a function that refreshes a web page.
the one here was tested on Chrome.
Disclaimer: this will remove the page reloading functionality entirely for a given page.

Disabling a link tag using JavaScript on my printable page

I have a script that creates a printable page by copying the HTML across and then doing some manipulation, such as disabling the buttons on the page, on page load.
I also want to disable the links on the page. I don't really mind if they look like links still as long as they don't do anything, and don't give any JavaScript errors!
The anchor tag doesn't seem to have a disabled attribute...
Unfortunately, I can't use jQuery, so JavaScript only please!
Edit: I want to disable the links, buttons etc on the page so that when the 'printable page' opens in another window, the user cannot mess with it by clicking buttons or links. I want it to essentially be a 'frozen snapshot' of the page that they want to print.
Setting their href to # and overwriting the onclick event should do the trick.
var links = document.getElementsByTagName("A"), j;
for (j = 0;j < links.length; j += 1) {
links[j].href = '#';
links[j].onclick = function () {
return false;
};
}
Why can't you use jQuery? So much nicer...
$('a').each(function(){this.onclick=function(){return false}});
Anyway here is a normal javascript way. Smaller than above and you also don't need to modify the links... by defining the onclick function to return false it will not visit the href:
var anchors = document.getElementsByTagName("a");
for (i = 0; i < anchors.length; i++)
anchors[i].onclick = function(){return false};
There is also an array of links in the document object. While I've never tried, I believe you can set them too.
for (i=0;i<document.links.length;i+=1) {
document.links[i]=='#';
}

Javascript: How to reload entire frameset onload() event including frame where Javascript is called

Is there a way to reload an entire frameset using Javascript onload() event?
function logout() {
/* ... */
// reload entire frame
top.location.reload();
}
<body onload="logout()">
This cause all frames to reload but the URL of the frame where this was called didn't changed to the URL specified in the framset.
As I understood it, you want to reload each frame in a frameset using the original URL as stated in <frame src="...">.
This little function can do that (put it into the document holding the frameset):
this.reloadChildFrames = function()
{
var allFrames = document.getElementsByTagName("frame");
for (var i = 0; i < allFrames.length; i++)
{
var f = allFrames[i];
f.contentDocument.location = f.src;
}
}
You are then able to call that function from within any child frame:
top.reloadChildFrames()
Of course, this can only work when all frames come from the same origin.

Categories