SitePoint Sponsor

User Tag List

Results 1 to 21 of 21
  1. #1
    SitePoint Member
    Join Date
    Jan 2011
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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"

    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.

    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 :P
    Code:
            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.

  2. #2
    SitePoint Evangelist TommiChi's Avatar
    Join Date
    Oct 2008
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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>

  3. #3
    SitePoint Member
    Join Date
    Jan 2011
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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>

  4. #4
    SitePoint Evangelist TommiChi's Avatar
    Join Date
    Oct 2008
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  5. #5
    SitePoint Member
    Join Date
    Jan 2011
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by TommiChi View Post
    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?
    Code:
    newWin.setTimeout("alert(document.body.innerHTML)",3250);
    anyway, so far you have been a big help :P

  6. #6
    SitePoint Evangelist TommiChi's Avatar
    Join Date
    Oct 2008
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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>

  7. #7
    SitePoint Member
    Join Date
    Jan 2011
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <!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...

  8. #8
    Just Blow It bronze trophy
    DaveMaxwell's Avatar
    Join Date
    Nov 1999
    Location
    Mechanicsburg, PA
    Posts
    7,267
    Mentioned
    116 Post(s)
    Tagged
    1 Thread(s)
    Are you getting errors? How is it not working?

    You need to be specific if you'd like some help....
    Dave Maxwell - Manage Your Site Team Leader
    My favorite YouTube Video! | Star Wars, Dr Suess Style
    Learn how to be ready for The Forums' Move to Discourse

  9. #9
    SitePoint Member
    Join Date
    Jan 2011
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DaveMaxwell View Post
    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.

  10. #10
    Just Blow It bronze trophy
    DaveMaxwell's Avatar
    Join Date
    Nov 1999
    Location
    Mechanicsburg, PA
    Posts
    7,267
    Mentioned
    116 Post(s)
    Tagged
    1 Thread(s)
    Remove the , false); after the closing bracket right before the script tag. That will get you past the error....
    Dave Maxwell - Manage Your Site Team Leader
    My favorite YouTube Video! | Star Wars, Dr Suess Style
    Learn how to be ready for The Forums' Move to Discourse

  11. #11
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Location
    Oskarshamn, Sweden
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    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?

  12. #12
    SitePoint Member
    Join Date
    Jan 2011
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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!

  13. #13
    Just Blow It bronze trophy
    DaveMaxwell's Avatar
    Join Date
    Nov 1999
    Location
    Mechanicsburg, PA
    Posts
    7,267
    Mentioned
    116 Post(s)
    Tagged
    1 Thread(s)
    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.
    Dave Maxwell - Manage Your Site Team Leader
    My favorite YouTube Video! | Star Wars, Dr Suess Style
    Learn how to be ready for The Forums' Move to Discourse

  14. #14
    SitePoint Member
    Join Date
    Jan 2011
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Quote Originally Posted by DaveMaxwell View Post
    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.

    And damn, fast replies...

  15. #15
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,836
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by zassadgh2 View Post
    Is there anyway to do it on other domains then your own? or is javascript not powerful enough?
    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.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  16. #16
    SitePoint Member
    Join Date
    Jan 2011
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "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."

    So ajax also has a security issue... any other ideas

  17. #17
    Just Blow It bronze trophy
    DaveMaxwell's Avatar
    Join Date
    Nov 1999
    Location
    Mechanicsburg, PA
    Posts
    7,267
    Mentioned
    116 Post(s)
    Tagged
    1 Thread(s)
    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?
    Dave Maxwell - Manage Your Site Team Leader
    My favorite YouTube Video! | Star Wars, Dr Suess Style
    Learn how to be ready for The Forums' Move to Discourse

  18. #18
    SitePoint Member
    Join Date
    Jan 2011
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Vbs

  19. #19
    Just Blow It bronze trophy
    DaveMaxwell's Avatar
    Join Date
    Nov 1999
    Location
    Mechanicsburg, PA
    Posts
    7,267
    Mentioned
    116 Post(s)
    Tagged
    1 Thread(s)
    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.
    Dave Maxwell - Manage Your Site Team Leader
    My favorite YouTube Video! | Star Wars, Dr Suess Style
    Learn how to be ready for The Forums' Move to Discourse

  20. #20
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Location
    Oskarshamn, Sweden
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    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 :-) It should at least give you an idea about how to use it...
    Code:
    <!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, "&lt;") + "</pre>";
    		});
    
    		return false;
    	};
    }());
    </script>
    </body>
    </html>

  21. #21
    SitePoint Member
    Join Date
    Jan 2011
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by CletusSpuckler View Post
    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 :-) It should at least give you an idea about how to use it...
    Code:
    <!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, "&lt;") + "</pre>";
    		});
    
    		return false;
    	};
    }());
    </script>
    </body>
    </html>
    Hmmm thx, now i just want to know if it's possible, can i execute
    document.cookie="pid=2);
    so that it will show a page that requires that cookie?

    so run a command that sets cookie for site and then runs the innerhtml?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •