|
| |
|
|
sharkie
Posts: 54 Joined: 6/20/2002 From: Washington DC USA Status: offline
|
determine current page in iframe - 5/10/2005 16:22:38
I have ten pages that load into an iframe. How can I determine which page is currently in the iframe? I want to code something like: IF current page = pageone.htm THEN DO THIS. IF current page = pagetwo.htm THEN DO THE OTHER. etc....
|
|
|
|
dpf
Posts: 7126 Joined: 11/12/2003 From: India-napolis Status: offline
|
RE: determine current page in iframe - 5/10/2005 16:32:09
what selects which page to load?
_____________________________
Dan
|
|
|
|
sharkie
Posts: 54 Joined: 6/20/2002 From: Washington DC USA Status: offline
|
RE: determine current page in iframe - 5/10/2005 16:44:47
The user pushes a button that loads one of the pages into the iframe. Each page has a separate button. I know that I can capture a reference to the page in a text field at that time but I was wondering if I can determine the cuurent page directly with something like ....document.iframe.content....
|
|
|
|
dpf
Posts: 7126 Joined: 11/12/2003 From: India-napolis Status: offline
|
RE: determine current page in iframe - 5/10/2005 17:01:24
perhaps if you give the iframe a name <iframe name="thisFrame".. then you can use dot syntax to get to it If (document.thisFrame.src ="page1.htm"){ do this; } elseif (document.thisFrame.src="page2.htm"){ do something esle; }
_____________________________
Dan
|
|
|
|
sharkie
Posts: 54 Joined: 6/20/2002 From: Washington DC USA Status: offline
|
RE: determine current page in iframe - 5/10/2005 17:26:51
That seems to be working. I tried that before but it did not work. Perhaps I was using == instead of = (javascript). I will test it further tomorrow. Thanks for your quick and accurate help.
|
|
|
|
kevin3442
Posts: 197 Joined: 1/25/2005 From: Nebraska, USA (home of the once-mighty Huskers) Status: offline
|
RE: determine current page in iframe - 5/10/2005 22:11:21
quote:
Perhaps I was using == instead of = (javascript). Oooo... careful now! Just a few comments (hope you don't mind Dan): (1) I don't think document.thisFrame.src will be the correct syntax... it'll probably generate a "document.thisFrame has no properties" error. I don't thing you can access the iframe's .src like that. You could, however, get the object id, and access the .src that way. (2) Once you get the syntax worked out to access the object, you'll want to be careful which operator you use in your conditional test. == is the operator you should use when checking for equality in a conditional. = is the assignment operator. Swapping them can cause you no end of headaches! For example, by using if (someObjectOrAnother.src = "page1.htm") you would actually be testing the outcome of an assignment. If the assignment is successful (i.e., if someObjectOrAnother.src is actually assigned a value of "page1.html" with the = operator), then the "if" will evaluate to true. In this sort of situation, the mistaken assignment is almost always successful, and your condition always evaluates to true. Then, after an hour of pulling out your hair, you finally spot the = where a == should have been. Very frustrating! As another example of potential headache: by using =, once you get the object syntax worked out, you might actually end up resetting the source in the iframe rather than testing it! (3) How you can access the source of the iframe will depend on how you created the iframe. If the iframe is hardcoded in your html code, it becomes part of the browser's frames array and can be accessed through that (by using .location, because .src will not work in that context). For example: window.frames['iframeName'].location would contain the complete url for the iframe's source, as long as you set the name attribute in the <iframe> tag, as Dan said. But if the iframe was generated dynamically at runtime (e.g., created through other javascript), it probably will not become part of the frames array. But never fear, DOM is here! You could also access the iframe object like this: var obj = document.getElementById('iframeName') And... obj.src is accessible with this approach. This method should work for DOM browsers, no matter how the iframe was created. (4) I'd be wary of testing direct equality with ==. If you use the frames['iframeName'].location approach, the returned string will contain not only the page's document name, but the path as well. If you use the DOM method, then the .src will contain whatever the setting for the src attribute was in the <iframe> tag or what it was when it was last set through script; which may or may not be just the page's file name. If the src used to set the iframe page contained a path, then so will the returned value of .src. For these reasons, it might be better to test whether the returned value of the page currently in the iframe contains the page name that you're looking for; that way you don't have to worry about an exact match. You can do that with the the indexOf() method of a sting object. OK... having said all that... try this: In your <iframe> tag, set the name attribute and the id attribute, and use the same value for both. (It's best to set both, to increase cross-browser compatibility. E.g., I believe Moz needs the id attribute rather than the name attribute to make the iframe part of the object hierarchy). Like so: <iframe ... name="main" id="main"> Now the iframe is accessible in the object hierarchy! And here's a function you can use to test any iframe's source:
function pageInIframe(pageName, iframeName)
{
var iframeSrc = document.getElementById(iframeName).src;
if (iframeSrc.indexOf(pageName) > -1) return true;
return false;
} When calling the function, you pass two parameter (both are passed as strings): pageName is the name of the file. Is it in the iframe or not? iframeName is the name/id of the iframe you want to test. For example, to test whether a page named "page1.html" is currently loaded into the "main" iframe that we defined above, we'd call the function like so: pageInIframe('page1.html', 'main'); This function call would return true if the page is in the iframe; false if it is not. The function can be used with any iframe and any source page. In a conditional, it might be used as follows:
if (pageInIframe('page1.html', 'main')) {
// actions to take if page1.html is in the iframe
}
elseif (pageInIframe('page2.html', 'main')) {
// actions to take if page2.html is in the iframe
}
else {
// actions to take none of the pages are in the iframe
} OK... OK... so my "few" points turned out to be long winded. Sometimes I get on a roll and it's hard to stop, especially if I'm avoiding doing other things . I hope this made sense, and hope it helps. Holler if you have questions. Cheers, Kevin
< Message edited by kevin3442 -- 5/10/2005 23:31:40 >
|
|
|
|
dpf
Posts: 7126 Joined: 11/12/2003 From: India-napolis Status: offline
|
RE: determine current page in iframe - 5/10/2005 22:33:38
quote:
(hope you don't mind Dan): are you kidding? excellent job, kevin..excellent
_____________________________
Dan
|
|
|
|
kevin3442
Posts: 197 Joined: 1/25/2005 From: Nebraska, USA (home of the once-mighty Huskers) Status: offline
|
RE: determine current page in iframe - 5/10/2005 22:50:21
Thanks Dan.
|
|
|
|
sharkie
Posts: 54 Joined: 6/20/2002 From: Washington DC USA Status: offline
|
RE: determine current page in iframe - 5/11/2005 8:24:08
Kevin: Thank you so much for the time and effort that you put into your response. It is much more that I would expect from a forum member. I will review it and post the tested code later today or tomorrow. The odd thing is that ...document.iFrame_name.src... seems to work and does not give me an error. But I only tested very briefly before I left the office last night. Thanks again.
|
|
|
|
sharkie
Posts: 54 Joined: 6/20/2002 From: Washington DC USA Status: offline
|
RE: determine current page in iframe - 5/11/2005 17:49:39
Kevin: You were right about the ....document.iframeName.src... However, I cannot seem to get this to work. It does not seem to be reading the page name and/or the frame name. This is setup with a button that calls navPro(); and it always returns 'maybe'. There are no error messages. Perhaps it is returning more than 'history.asp'? I did try the page with the path and still no luck. I have the iframe name & id = case_inline and the history.asp page is in the iFrame. Please tell me where my obtuseness lies. ---------code------------ function pageInIframe(pageName, iframeName) { var iframeSrc = document.getElementById(iframeName).src; if (iframeSrc.indexOf(pageName) > -1) return true; return false; } function navProfile(){ if (pageInIframe('history.asp', 'case_inline')) {document.sidemenu.pag_set.value = 'No';} else {document.sidemenu.pag_set.value = 'maybe';} } ------end code----------------
|
|
|
|
kevin3442
Posts: 197 Joined: 1/25/2005 From: Nebraska, USA (home of the once-mighty Huskers) Status: offline
|
RE: determine current page in iframe - 5/11/2005 17:52:20
Hi sharkie, Can you please post the code you use to define the iframe? Kevin
|
|
|
|
sharkie
Posts: 54 Joined: 6/20/2002 From: Washington DC USA Status: offline
|
RE: determine current page in iframe - 5/12/2005 8:44:00
Sorry, Kevin. I should have included it. -----------CODE---------------- <IFRAME NAME="case_inline" ID="case_inline" onLoad="document.sidemenu.pag_set.value='Yes';" MARGINWIDTH="3" MARGINHEIGHT="3" HEIGHT="500" WIDTH="613" BORDER="0" FRAMEBORDER="0"> </IFRAME> ---------END CODE----------------
|
|
|
|
kevin3442
Posts: 197 Joined: 1/25/2005 From: Nebraska, USA (home of the once-mighty Huskers) Status: offline
|
RE: determine current page in iframe - 5/12/2005 17:32:30
Hi Sharkie, Sorry for the delayed reply; been at home sick for most of the day. I just had a look. The function seems to work properly in my tests here. One thing I noticed: In your <iframe> tag, you do not initially load a page (there's no src setting). So, I assume that the page gets loaded through some other script. Are you running your navProfile() function after the iframe is actually loaded with content? The way you have your navProfile() function coded, the pag_set form field (at least that's what I assume it is) will be set to 'maybe' if history.asp is NOT in the iframe; it is set to 'No' if history.asp IS in the iframe. is that what you want it to do??? One thing you can try, to track the processing, is temporarily modify the pageInIframe() function to tell you what the iframe src is when the function runs. Place an alert() in it, like so:
function pageInIframe(pageName, iframeName)
{
var iframeSrc = document.getElementById(iframeName).src;
alert(iframeSrc);
if (iframeSrc.indexOf(pageName) > -1) return true;
return false;
} That'll pop up an alert box containing the setting for the iframe's src attribute. That way, you'll know whether the function is correctly getting the iframe's src or not. If it is, then you can trace the problem further down stream. I'll be around for another hour or two before I have to throw in the towel for the day. Cheers, Kevin
|
|
|
|
dpf
Posts: 7126 Joined: 11/12/2003 From: India-napolis Status: offline
|
RE: determine current page in iframe - 5/12/2005 17:51:10
quote:
if you carry on like that you might end up being a moderator now hold on just a sec.... yea, sure kevin has a cape but I have stared at it for 5 minutes and not a single flutter like yours - how can he be a moderator with such an idle cape? in fact, doesnt that make him "incapeable" of being a moderator??
_____________________________
Dan
|
|
|
|
kevin3442
Posts: 197 Joined: 1/25/2005 From: Nebraska, USA (home of the once-mighty Huskers) Status: offline
|
RE: determine current page in iframe - 5/12/2005 20:20:56
quote:
ORIGINAL: dpf ...I have stared at it for 5 minutes and not a single flutter... Ahhh... but it is fluttering. It's currently on its high "capeacity" flutter setting, fluttering too fast for your eyes to track, thus appearing stationary. quote:
ORIGINAL: jaybee Good job Thanks jaybee. Much appreciated. Cheers, Kevin
|
|
|
|
dpf
Posts: 7126 Joined: 11/12/2003 From: India-napolis Status: offline
|
RE: determine current page in iframe - 5/13/2005 8:53:33
quote:
high "capeacity" flutter setting checkmate....<smile>
_____________________________
Dan
|
|
|
|
sharkie
Posts: 54 Joined: 6/20/2002 From: Washington DC USA Status: offline
|
RE: determine current page in iframe - 5/13/2005 9:34:46
Kevin: I removed the src="apagename" when I was testing it, in order to see if it was causing a problem. I forgot to replace it when posting the code. The test code is working this morning. I don't know why it suddenly decided to cooperate. Perhaps a cache memory issue. Thanks for your help. If you are ever in Washington DC, stop-by and I will treat you to lunch. But please leave your cape at home and travel by more conventional methods. I am two blocks from the White House and low-flying objects in this area tend to send white house and congressional staff scurrying into the streets.
|
|
|
|
kevin3442
Posts: 197 Joined: 1/25/2005 From: Nebraska, USA (home of the once-mighty Huskers) Status: offline
|
RE: determine current page in iframe - 5/16/2005 23:00:17
quote:
ORIGINAL: sharkie Thanks for your help. If you are ever in Washington DC, stop-by and I will treat you to lunch. You're welcome. Glad it worked. My "real" work actually takes me to the DC area fairly often. But I'm usually too busy working to take much time for fun. I will, however, keep your offer in mind. Cheers, Kevin
|
|
New Messages |
No New Messages |
Hot Topic w/ New Messages |
Hot Topic w/o New Messages |
Locked w/ New Messages |
Locked w/o New Messages |
|
Post New Thread
Reply to Message
Post New Poll
Submit Vote
Delete My Own Post
Delete My Own Thread
Rate Posts
|
|
|