Solved

Capturing Window URL; copying selected text.

Posted on 2004-08-05
12
744 Views
Last Modified: 2008-02-01
I have a webpage that opens two windows.

<p><a href="window1.htm" target="Window1Name">Window 1</a></p>
<p><a href="window2.htm" target="Window2Name">Window 2</a></p>

Window1.htm has many links and some text on it.

I want to write Javascript code (2 different functions) in Window2.htm that would

1. Capture the current URL of browser window "window1name" and paste it into a textbox called "URL" on window2.htm
2. Copy and paste any selected text in "window1name" and paste it into a textarea called "SelectedText" on window2.htm

Can anyone help me with this?
:) SD

PS1: Here is the Javascript code i found online that would copy and paste selected text into a textarea on the same page.
http://javascript.internet.com/page-details/copy-selected-text.html
PS2: Needless to say, I am willing to give more points if someone would give me a very comprehensive solution.
0
Comment
Question by:sciber_dude
12 Comments
 
LVL 36

Expert Comment

by:Zyloch
ID: 11731761
Hi

This will only work if both Window2 and Window1 were opened from the same window. Then, Window2 can have this code:

<script language="javascript">
<!--
function getFirstWindowURL(Window1Name) {
   if (!window.opener) {return("");}
   return(opener.Window1Name.location.href);
}
function copyit(Window1Name) {
  if (!window.opener) {return("");}
    var selectedText = opener.Window1Name.document.selection;
    if (selectedText.type == 'Text') {
        var newRange = selectedText.createRange();
        return(newRange.text);
    }
}
// -->
</script>

Then, you can get the location of Window1 by calling
var window1url = getFirstWindowURL(WINDOW1NAME);

To get the copied text, call it like this:
var copiedText = copyit(WINDOW1NAME);
document.FORMNAME.TEXTFIELDNAME.value = copiedText;



Regards,
Zyloch
0
 
LVL 11

Author Comment

by:sciber_dude
ID: 11732632
@ Zyloch -

I tried one of the functions you have give me, but i get an error on this line.
> opener.Window1Name.location.href

I have recreated the webpage at http://www33.brinkster.com/sbpress/main.htm

I would appreciate it if you could help me sort this out.

:) SD
0
 
LVL 6

Expert Comment

by:viola123
ID: 11732866
hi,

you should use :
   window.opener.location.href

because the opener is just that Window1Name. you cannot reference the same object twice


cheers
viola123
0
 
LVL 11

Author Comment

by:sciber_dude
ID: 11733168
nope.. the opener is the first page from where both window1 and window2 are opened.

I am trying to find the URL of window1 by writing the Javascript on window2.

:) SD

0
 
LVL 9

Accepted Solution

by:
nimaig earned 250 total points
ID: 11734155
Try this out. Copy the html code and save it in 3 different html files. Start from mail.html.


========================== main.html===========================
<html>
<script language="javascript">
      var newWindowHandlersArr = new Object();
      function openWindow(hrefstr,targetstr){
            newWindowHandlersArr[targetstr] = window.open(hrefstr,targetstr);
            alert(newWindowHandlersArr[targetstr]);
      }
</script>
<body bgcolor="#f7e4d7">
<form name="frm1" onsubmit="javascript:alert('submit called')">
<p><a href="javascript:openWindow('window1.html','Window1Name')">Window 1</a></p>
<p><a href="javascript:openWindow('window2.html','Window2Name')">Window 2</a></p>
</form>
</body>
</html>





========================== window1.html===========================
<html>
<body>
<form name="frm1">
      <input type="text" name="txt1">
</form>
</body>




========================== window2.html===========================
<html>
<head>
<script language="javascript">
      function getData(){
            var jsArrayObj = window.opener.newWindowHandlersArr;
            if(typeof(jsArrayObj) != "undefined"){
                  var winHandler = jsArrayObj["Window1Name"];
                  if (winHandler && winHandler.open && !winHandler.closed){
                        document.frm1.txtText.value=winHandler.document.frm1.txt1.value;
                        document.frm1.txtURL.value=winHandler.location.href;
                  }
            }
      }
</script>
</head>
<body>
<form name="frm1">
      url : <input type="text" name="txtURL">
      <br>text : <input type="text" name="txtText">
      <input type="button" value="Get Data" onclick="javascript:getData()">
</form>
</body>
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 11735172
Yes, this is my fault. I thought you could access the child window from child window, but it seems only the parent has any right to that.

Place these two functions in the parent window:

var theLoc = "";
var theSel="";
function getFirstWindowURL(Window1Name) {
   if (!Window1Name) {return("");}
   theLoc = eval(Window1Name+".location.href");
}
function copyit(Window1Name) {
  if (!Window1Name) {return("");}
    var selectedText = eval(Window1Name+".document.selection");
    if (selectedText.type == 'Text') {
        var newRange = selectedText.createRange();
       theSel = newRange.text;
    }
}

Then, in you should have this:
<p><a href="http://www.google.com" target="Google">Google</a></p>
<p><a href="window2.htm" target="Window2Name" onclick="getFirstWindowURL('Google');">Form</a></p>

Then, in window2.htm, have this:
<body onload="setTimeout('opener.copyit(\'Google\'),500);">

Then, to get the stuff, the copied text would be under
opener.theSel
and the window.location should be under
opener.theLoc

Regards
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 11

Author Comment

by:sciber_dude
ID: 11735674
@ nimaig -

Whoa! you are almost there with capturing the URL. I have recreated ur files at
http://www33.brinkster.com/sbpress/test/main.html

This works as long as I am on window1.html in the window1.

The problem however is that .. If I change this line in main.html
<p><a href="javascript:openWindow('window1.html','Window1Name')">Window 1</a></p>

TO

<p><a href="javascript:openWindow('http://www.google.com','Window1Name')">Google</a></p>

It gives me an error - Access is denied on line no. 9.

The error also occurs whenever i change the URL of the window 1 by manually typing it in.

I need this function to be able to pick up the URL of window1 (and this page wont be under my control)

Thanks for helping me and hoping to work this one out.
:) SD

@ Zyloch -
I will give u feedback in a few min @ ur solution.
:) SD
0
 
LVL 11

Author Comment

by:sciber_dude
ID: 11735797
@ Zyloch -

I still seem to be getting an error at this line.

   theLoc = eval(Window1Name+".location.href");

>>>Could u please paste the full code (Including HTML) for the parent window and the window2? <<<
Let us assume that window1's URL is http://www.google.com.

This is what I want the code to do..
1. On the parent window, I will first click the Google link
2. On the Google window, I will search for something.
3. Then I will click on the form link on the parent window
4. When I click on "Capture Google URL" button, it will automatically capture the URL of the google window.

(I dont mind if the parent window needs to have a hidden/not-hidden text box to first capture the URL of the google window which can later be accessed by the Form window).

I am doing all this for saving the user the extra effort of Copying the URL and pasting on the form :(

Let me create a different question about the "copy selected text" after we work out the capturing URL. This would help us concentrate on one task.

Thanks a million for helping out on this.
:) SD
0
 
LVL 36

Assisted Solution

by:Zyloch
Zyloch earned 250 total points
ID: 11737336
This is rather impossible with the format you have. Because the second child window is accessing another domain, it will not tell you the location.href and say Permission Denied. You need to somehow redirect the opener or something. Below is my test code:

main.html
===========================================
<html>
<head>
<title>Main</title>
<script language="javascript" type="text/javascript">
<!--

var allWindows = new Array;
var theLoc = "";
 
function openWindow(theURL, theName, theParam) {
   eval(theName+" = window.open(theURL, theName, theParam);");
   allWindows[allWindows.length] = theName;
   theLoc = theURL;
}

function getURL(WindowName) {
   for (var i=0;i<allWindows.length;i++) {
      if (allWindows[i]==WindowName) {
         if (eval(WindowName) && eval("!"+WindowName+".closed")) {
            theLoc = eval(WindowName+".location.href");
            return(theLoc);
         }
      }
   }
   return(theLoc);
}

// -->
</script>
</head>
<body>
<div align="center"><h1>Test</h1></div>
<p>Open the Google window and search for something. Open the second window and press the button.</p>
<a href="#" onclick="openWindow('http://www.google.com/','Google','width=300,height=400');">Google</a>
<a href="#" onclick="openWindow('test6.htm','Test','width=300,height=400,location=yes');">Test</a>
</body>
</html>


test6.htm
==================================
<html>
<head>
<title>SubWindow Testing</title>
<script language="javascript" type="text/javascript">
<!--

function getURL(WindowName) {
   if (window.opener && !window.opener.closed) {
      return(opener.getURL(WindowName));
   }
}

// -->
</script>
</head>
<body>
<p>This is testing so don't get crazy here.</p>
<input type="button" name="getURLButton" id="getURLButton" value="Get URL" onclick="alert(getURL('Google'));">
<input type="text" name="finalURL" id="finalURL" value="">
</body>
</html>


There might be a workaround method that will work close to what you want. Post if you want us to try to come up with one.

Regards,
0
 
LVL 11

Author Comment

by:sciber_dude
ID: 11739172
@ Zyloch -

:( Permission Denied.

Here is the recreation of ur files: http://www33.brinkster.com/sbpress/main.htm

Do u think it will help if i have both test6.htm and google webpage as part of a page that has frames? :-?

There must be a way! :-|

Thanks for ur patience,
:) SD
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 11739756
If both are of the same page, even with frames, it should work fine. With Javascript alone, at least, you cannot get the location of an external page. If both test6.htm and the google webpage are in one page, even in different frames, it should work.
0
 
LVL 11

Author Comment

by:sciber_dude
ID: 11854397
Well. I decided not to pursue this course of action. So I guess my question is moot.

But thanks fellas for all your help.
:) SD
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now