Run javascript in window.open url field

Hello, I just started using javascript.
I am having problems with using window.open for my purpos

Here is my “code”


first navigate to a website
window.open('http://google.com/','windowname1','width=400,height=400')

Then execute this command for same window.
window.open('javascript:alert(document.body.innerHTML)','windowname1','width=400,height=400')

now the problem is that it’s not running “javascript:alert(document.body.innerHTML”.
the window.open to google works fine, the problem is i can’t get it to execute the javascript:alert.:mad:

why is that? is there another way of doing this?

//my goal is to open a window, then execute a javascript in the same window.

also I am using vb .net with javascript. if u had a vb .net solution :stuck_out_tongue:

        Dim strScript As String = "<script language=javascript>window.open('http://google.com/','windowname1','width=400,height=400')</script>"

        ClientScript.RegisterClientScriptBlock(Me.GetType(), "strScript", strScript)

        strScript = "<script language=javascript>window.open('window.open('javascript:alert(document.body.innerHTML)','windowname1','width=400,height=400')</script>"

        ClientScript.RegisterClientScriptBlock(Me.GetType(), "strScript", strScript)

All help is appreciated.:slight_smile:

You find below a simple example of what you are trying to do:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv=“Content-Type” content=“text/html; charset=utf-8”>
<title>Test Page</title>
</head>
<body>

<a>
click me
</a>
<script>
document.getElementsByTagName(‘a’)[0].addEventListener(‘click’, function() {
var newWin = window.open(‘http://www.google.com’);
newWin.alert(‘I am a new window’);
});
</script>
</body>
</html>

The problem is that the script runs on the current browser window, not the new. my goal is to get the innerHTML of the new opened window.

<script type=“text/javascript”>
function gethtmlfromnewwindow() {
var newWin = window.open(‘http://www.google.com’);
newWin.alert(document.body.innerHTML);
}
</script>

In tbat case you need to wait for the new window to load before firing the alert, but at that point I think you an only fire the alert in the parent, instead of the window that just opened

tryed using this command for delay, but it is not working? :mad:

newWin.setTimeout("alert(document.body.innerHTML)",3250);

anyway, so far you have been a big help :stuck_out_tongue:

You’ll run into permissions issues when trying to grab content from external sites. However, on local files and sub-domains it should be all gravy. I put together a short example you can play with to get the effect you want:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv=“Content-Type” content=“text/html; charset=utf-8”>
<title>Test Page</title>
</head>
<body>

<a id=“a”>click me</a>
<iframe id=“newWin” src=“” width=“0” height=“0” frameborder=“0”></iframe>
<script>
document.getElementsByTagName(‘a’)[0].addEventListener(‘click’, function() {
window.newWin = window.open(url);
var prevLoaded = 0;
var pageLoaded = setInterval(function() {
var isLoaded = newWin.length || -1;
if(isLoaded > 0 && isLoaded != prevLoaded) {
prevLoaded = isLoaded;
}
if(isLoaded > 0 && isLoaded == prevLoaded) {
alert(newWin.document.body.innerHTML);
clearInterval(pageLoaded);
}
}, 1000);
}, false);
</script>
</body>
</html>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<script type="text/javascript">
function workffs() {
alert("it is not even working here... not showing when the code below is there.");
window.newWin = window.open('http://www.google.com');
var prevLoaded = 0;
var pageLoaded = setInterval(function() {
var isLoaded = newWin.length || -1;
if(isLoaded > 0 && isLoaded != prevLoaded) {
prevLoaded = isLoaded;
}
if(isLoaded > 0 && isLoaded == prevLoaded) {
alert(newWin.document.body.innerHTML);
clearInterval(pageLoaded);
}
}, 1000);
}, false);
</script>
</head>

<body>

<input name="ddd" type="button" onclick="workffs()" value="dddd" />
<iframe id="newWin" src="" width="0" height="0" frameborder="0"></iframe>
</body>
</html>

it is not working? I am a bit new to javascript so i don’t know whats wrong…

Are you getting errors? How is it not working?

You need to be specific if you’d like some help…

It is not running any of the commands, if i remove all the code except the alert in the start, the alert works just fine(so calling the function works…), but when adding the other code it stops working, including the the alert in the top of the script.

Remove the , false); after the closing bracket right before the script tag. That will get you past the error…

Like TommiChi pointed out earlier, you won’t be able to get the content from external sites this way anyhow… It might work if you open one of your own pages on the same domain, but your Google example will not work.

What are you trying to do anyway? Some form of web scraping?

any way, yep it is working. thank you for all your help, everbody.

So my final question is…

Is there anyway to do it on other domains then your own? or is javascript not powerful enough?

anyone who got an idea? I only need this function, it’s the main idea of the site form I am making.

ways of doing it.

I need to “visit” a website in a browser window, then change the url(src) to “javascript:alert(document.body.innerHTML)” link in the url bar. // remember i do not own the domain that the “javascript:alert(document.body.innerHTML)” will target.

Thanks for all the help so far!:wink:

The only way to do it is to pull the external page down through a server based ajax call, then display that page. Not the best solution, but it does work.

Thanks! I will try to see if I can figure out how to do that.:wink:

And damn, fast replies…:slight_smile:

It isn’t a matter of JavaScript not being powerful enough. It is a matter of security. If JavaScript could do it then it could be used to steal credit card numbers and passwords from banking sites. It is deliberately blocked to prevent misuse.

“Including an external page using Ajax is seamless, and even works if the external page contains items like CSS and JavaScript. There is a big catch, however. For security reasons, you can only include an external page that’s on the same domain as the page including it. In fact, the two domains must match exactly, down to the “www” (or lack thereof) part. Due to this, if you’re specifying an absolute URL to the external page on your domain using the above function, it’s best to dynamically construct the domain portion of the URL so it matches exactly that of the including page.”:mad:

So ajax also has a security issue… any other ideas

There is a difference between using a server side ajax call and a client-side ajax call - client side has that restriction, server side does not. You do need to be aware of the danger however and be sure you can parse the returned page correctly.

How it is done depends on the language you use, so what server side language are you using? PHP? .net/C#? VBScript?

Vbs

Hmmm. Let me see if I have my code around from the last time I did it via vbscript-land. It’ll be a while since I’ve just bought a new machine and I’m still transitioning files from the old to the new.

In the meantime, this provides the framework (notice the caveat, though).

Otherwise, try searching via google and see if that doesn’t get you anywhere. You can also search for XHR, which should lead you in the right direction.

You could check out YQL if you don’t want to write the server side code yourself… Here’s an example that I wrote kinda quickly, so there’s no error handling and possibly things could be done in a better way. But I won’t spend any more time on it tonight :slight_smile: It should at least give you an idea about how to use it…


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Scraping</title>
</head>

<body>
<form action="">
	<div>
		<label for="url">URL</label>
		<input id="url" name="url" type="url" />
		<input type="submit" value="Get content" />
	</div>
</form>
<div id="result"></div>

<script>
var Scraper = (function () {
	var callbackIndex = 0;

	function createCallback(callback, currentScript) {
		callbackIndex += 1;
		var callbackName = "c" + callbackIndex;
		Scraper[callbackName] = function (data) {
			var content = "";
			if (data.results) {
				content = data.results[0];
			}
			callback(content);

			document.body.removeChild(currentScript);
			delete Scraper[callbackName];
		};
		return "Scraper." + callbackName;
	}

	function get(url, callback) {
		var query = "select * from html where url = '" + url + "'",
			src = "",
			s = document.createElement("script"),
			callbackName = createCallback(callback, s);

		src = "http://query.yahooapis.com/v1/public/yql?q=" + encodeURIComponent(query) + "&format=xml&callback=" + callbackName;
		s.setAttribute("src", src);
		document.body.appendChild(s);
	}

	return {
		get: get
	};
}());

(function () {
	var form = document.getElementsByTagName("form")[0];
	form.onsubmit = function () {
		var res = document.getElementById("result");
		res.innerHTML = "Loading...";

		Scraper.get(this.url.value, function (content) {
			res.innerHTML = "<pre>" + content.replace(/</g, "<") + "</pre>";
		});

		return false;
	};
}());
</script>
</body>
</html>