Link to home
Start Free TrialLog in
Avatar of sciber_dude
sciber_dudeFlag for United States of America

asked on

Capturing Window URL; copying selected text.

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.
Avatar of Zyloch
Zyloch
Flag of United States of America image

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
Avatar of sciber_dude

ASKER

@ 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
Avatar of viola123
viola123

hi,

you should use :
   window.opener.location.href

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


cheers
viola123
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

ASKER CERTIFIED SOLUTION
Avatar of nimaig
nimaig
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
@ 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
@ 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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@ 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
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.
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