sciber_dude
asked on
Capturing Window URL; copying selected text.
I have a webpage that opens two windows.
<p><a href="window1.htm" target="Window1Name">Windo w 1</a></p>
<p><a href="window2.htm" target="Window2Name">Windo w 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.
<p><a href="window1.htm" target="Window1Name">Windo
<p><a href="window2.htm" target="Window2Name">Windo
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.
ASKER
@ Zyloch -
I tried one of the functions you have give me, but i get an error on this line.
> opener.Window1Name.locatio n.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
I tried one of the functions you have give me, but i get an error on this line.
> opener.Window1Name.locatio
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
hi,
you should use :
window.opener.location.hre f
because the opener is just that Window1Name. you cannot reference the same object twice
cheers
viola123
you should use :
window.opener.location.hre
because the opener is just that Window1Name. you cannot reference the same object twice
cheers
viola123
ASKER
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
I am trying to find the URL of window1 by writing the Javascript on window2.
:) SD
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(Window1N ame) {
if (!Window1Name) {return("");}
theLoc = eval(Window1Name+".locatio n.href");
}
function copyit(Window1Name) {
if (!Window1Name) {return("");}
var selectedText = eval(Window1Name+".documen t.selectio n");
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
Place these two functions in the parent window:
var theLoc = "";
var theSel="";
function getFirstWindowURL(Window1N
if (!Window1Name) {return("");}
theLoc = eval(Window1Name+".locatio
}
function copyit(Window1Name) {
if (!Window1Name) {return("");}
var selectedText = eval(Window1Name+".documen
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><a href="window2.htm" target="Window2Name" onclick="getFirstWindowURL
Then, in window2.htm, have this:
<body onload="setTimeout('opener
Then, to get the stuff, the copied text would be under
opener.theSel
and the window.location should be under
opener.theLoc
Regards
ASKER
@ 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:openWindo w('window1 .html','Wi ndow1Name' )">Window 1</a></p>
TO
<p><a href="javascript:openWindo w('http://www.google.com','Window1Name')">Goog le</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
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:openWindo
TO
<p><a href="javascript:openWindo
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
ASKER
@ Zyloch -
I still seem to be getting an error at this line.
theLoc = eval(Window1Name+".locatio n.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
I still seem to be getting an error at this line.
theLoc = eval(Window1Name+".locatio
>>>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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@ 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
:( 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.
ASKER
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
But thanks fellas for all your help.
:) SD
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(Window1N
if (!window.opener) {return("");}
return(opener.Window1Name.
}
function copyit(Window1Name) {
if (!window.opener) {return("");}
var selectedText = opener.Window1Name.documen
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(WINDOW1N
To get the copied text, call it like this:
var copiedText = copyit(WINDOW1NAME);
document.FORMNAME.TEXTFIEL
Regards,
Zyloch