Reputation: 1142
I'd like to open another web page from javascript (like default navigating, not fullscreen iframe or window.open) and run some javascript code upon loading it.
Steps:
is there any way to achieve this? the only way I remember would be emulating this by loading the page using ajax, and replacing document.body.innerHtml, then running the function, but that would not change location.href, so e.g. the back button or bookmarks wouldn't work. also relative links had to be rewritten at loading, etc...
PS: I know that would be some ugly XSS, but it's needed for example when writing bookmarklets that load a page and fill in a form automatically.
Upvotes: 1
Views: 293
Reputation: 1142
OK, reserch results:
Impossible methods:
Overkill methods:
Remaining method 1, requiring user interaction:
(function(){
if(self.location.href!=targetlocation){
window.alert("Please run this bookmarklet again upon loading the page.");
self.location.href=targetlocation;
}else{
doSomething();
}
return false;
})();
Remaining method 2, doing some nasty proxy stuff:
Example:
<?php //USAGE: ?uri=http://www.google.com&search[0]=L2dvb2dsZS8K&replace[0]=dGVzdAo=
$page=file_get_contents($_GET["uri"]);
$count = min(count($_GET["search"]),count($_GET["replace"]),100);
for ($i = 0; $i < $count; $i++) {
$page=preg_replace(base64_decode($_GET["search"][$i]),
base64_decode($_GET["replace"][$i]), $page);
}
echo $page;
?>
Upvotes: -1
Reputation: 10090
You can do all this stuff if you load the other page from your own server:
Say you want to load http://other.com in your site http://mine.org You write a tiny serverside script that you can call like this: http://mine.org/load.php?site1 (with the urls to all the sites you want to load listed inside load.php or in some database)
but now your site has the security problem: javascript embedded in http://other.com is run in your sites context.
Upvotes: 1
Reputation: 20394
While there could be some legitimate use cases, for security reasons you can't do this unless the new page is on the same domain.
What you can do is to write a browser extension if the target browser has extensions support,
Or tell users to open the target page, and use your bookmarklet on that context.
Upvotes: 1
Reputation: 141829
No, you can't do that. That would allow you to do things like steal cookies for session hijacking behind the scenes, so no browsers allow you to do it at all.
Upvotes: 2