Solved

Passing parent URL string to javascript

Posted on 2007-03-28
14
871 Views
Last Modified: 2008-01-09
I need to be able to pass the parent url's string into a piece of javascript that is contained in an iframe on the same page

In particular, with the following

<script type="text/javascript">
digg_url = 'WEBSITE_URL';
digg_bgcolor = '#ff9900';
digg_skin = 'compact';
</script>
<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script>

WEBSITE_URL needs to be the parent url programmitically, so that the Iframe can be used on any page

Anyone know how this can be accomplished?
0
Comment
Question by:cdr21
  • 6
  • 3
  • 3
  • +1
14 Comments
 
LVL 5

Expert Comment

by:Gitcho
ID: 18812558
from within the iframe, use parent.location.href.   eg  :
alert(parent.location.href);
0
 
LVL 7

Expert Comment

by:RealSnaD
ID: 18812567
digg_url = document.URL;
0
 
LVL 5

Expert Comment

by:Gitcho
ID: 18812576
if you use document.URL within the <iframe>, it will give you the URL of the iframe, not the parent.
0
 
LVL 7

Expert Comment

by:RealSnaD
ID: 18812603
Gitcho, good to know!
0
 
LVL 22

Accepted Solution

by:
neeraj523 earned 250 total points
ID: 18814766
I guess Gitcho is right..

more precisely u need to edit ur code like this

<script type="text/javascript">
//////////
digg_url = parent.location.href;
//////////////
digg_bgcolor = '#ff9900';
digg_skin = 'compact';
</script>
<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script>

neeraj523
0
 

Author Comment

by:cdr21
ID: 18816619

Unfortunately, even

digg_url = parent.location.href;

Is not working in this case.

The parent URL I'm testing with is:

http://www.kait8.com/global/Story.asp?s=6289742

The iframe block is just below the story body, titled 'Share this Story'

Any more ideas

-cdr
0
 
LVL 5

Assisted Solution

by:Gitcho
Gitcho earned 250 total points
ID: 18817202
ahhh ... ok ... I could be wrong, but it appears your <iframe> source is from a different domain.  Parent window information isn't available when the parent window is on a different domain - this is a security restriction by design ... that's why you're getting the "uncaught exception: Permission denied to get property Location.href" browser errors .

If you need your diggthis button in the iframe, you'll have to hard-code the referring href, because you won't be able to get it with javascript.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:cdr21
ID: 18821057


In this case, within this content management application I am 'tied' to, I really need to somehow get the parent URL info into the Iframe by passing a parameter of some sort.

If not javascript itself, can ASP or PHP be used to 'pull' 'referring' URL info and be transferred to a javascript variable once inside the Iframe?
0
 
LVL 22

Expert Comment

by:neeraj523
ID: 18821209

Alternatively, you can pass the parnet URL into frame page as querystring().. Which u can read in frame page using request object or even using javascript..

something like this in parent page

<iframe src="myFramePage.asp?parentUrl=<%=Request.ServerVaraibles("URL")%>"></iframe>

dont know if it help u out..

neeraj523
0
 

Author Comment

by:cdr21
ID: 18858523
thanks for your idea neeraj523... I finally found some time to try it, but it only passes the URL of the iframe itself again...

Alternatively, I can just construct a string of the form:

http://digg.com/submit?phase=2&url=example.com&title=TITLE&bodytext=DESCRIPTION&topic=TOPIC and use a button in the iframe to submit it.
How do I use parent.location.href to in pass the parent url in the url=example.com portion of the querystring above?

0
 
LVL 22

Expert Comment

by:neeraj523
ID: 18863577

cdr21.. i still believe the solution i provided should work.. <%=Request.ServerVaraibles("URL")%> should give the parne page url if written in the parnet page..

Make sure u r writting it in the parent page where u adding iframe
0
 

Author Comment

by:cdr21
ID: 18866295
I tried using ASP and the ServerVariables object in the querystring several different ways from the parent page like:

<IFRAME style="WIDTH: 341px; HEIGHT: 160px" name=ShareThisBox marginWidth=0 marginHeight=0 src="http://go.kait8.com/sharethis/K8is_sharethis2.asp?parentURL=<%=Request.ServerVariables("URL")%>" frameBorder=0 noResize width=400 scrolling=no height=160></IFRAME>

The following string appears in Digg.com as the one to 'share':
http://go.kait8.com/sharethis/K8is_sharethis2.asp?parentURL=

In the parent frame I have also tried enclosing the URL parameter in single quotes, like ('URL') and no quotes (URL)... the same string above keeps appearing...

Since the string is cut off where the first ASP marker appears, I'm assuming that the web content platform I'm working with does not support passing ASP marked up info in a query string.
0
 

Author Comment

by:cdr21
ID: 18866344
Also this week I have tried to circumvent the cross-domain security that Gitcho mentioned earlier.  

I moved the iframe to a subdomain and set the javascipt document.domain in the iframe to:

document.domain="kait8.com";

Still no luck...
0
 

Author Comment

by:cdr21
ID: 18885203

I was able to finally it working using the parent.location.href variable assignment and moving the  iframe to a a subdomain location

thanks all!

-cdr
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

744 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

15 Experts available now in Live!

Get 1:1 Help Now