Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Passing parent URL string to javascript

Posted on 2007-03-28
14
Medium Priority
?
920 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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:Yury Merezhkov
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 7

Expert Comment

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

Accepted Solution

by:
neeraj523 earned 1000 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 1000 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
 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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…

722 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