473,769 Members | 3,567 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling JS function from within iframe tag

I'm new to Javascript and to html and am trying to make the following
code snippet work but it doesn't. It refuses to call the getPage()
function and I always get a 404 error. I know the code is getting as
far as the iframe section as other parts of the html code work
correctly. But
it is not executing any code within the getPage() function as some
"hello world" writes within getPage() wouldn't print.

-----------------------------------------------------------------------------------
..
..
..

<script language="javas cript" type="text/javascript">

function getPage() {
<!--
.... some code here
return pageURL;
//-->
}
</script>
</head>

<body>
<iframe src="getPage()" name="whatever" width=100% height=600
frameborder="0" scrolling="yes" > </iframe>
..
..
..
-------------------------------------------------------------------------------

Can anyone help what the syntax error is here, or what I am trying
to do is not possible? Thanks and please no flames.

Dec 2 '05 #1
13 6713

ukrbend wrote:
I'm new to Javascript and to html and am trying to make the following
code snippet work but it doesn't. It refuses to call the getPage()
function and I always get a 404 error. I know the code is getting as
far as the iframe section as other parts of the html code work
correctly. But
it is not executing any code within the getPage() function as some
"hello world" writes within getPage() wouldn't print.

-----------------------------------------------------------------------------------
.
.
.

<script language="javas cript" type="text/javascript">
The language is deprecated and unnecessary. The type attribute will
suffice.
function getPage() {
<!-- [...] //-->
Why do you have html comment tags inside your function? That is
definitely an error. Remove those immediately.
<iframe src="getPage()" ></iframe>


You are incorrectly using the src attribute. The value of the src
attribute should be the URL of the document to show in the iframe.

Try the following instead since you are just beginning:

After you fix the above mentioned errors, create a second html, call it
pageTwo.html. Afterwards, move your javascript into pageTwo.html. To
invoke the function call, place the following somewhere in the body:

<script type = "text/javascript">
getPage();
</script>

And finally, for your iframe in your pageOne.html, change the value of
the src to be as follows:

<iframe src = "pageTwo.html"> </iframe>

Dec 2 '05 #2
Thanks for the reply. Regarding some of your points, why are the html
comment tags there? They are there in case the user has an older
browser that doesn't support javascript. Several examples of this can
be found here:

http://www.prestwood.com/community/w...ns/beg_js.html
http://www.engin.umd.umich.edu/CIS/c...pt/hellow.html
http://www.webreference.com/js/scripts/basic_date/

Second, you said the src attribute should be the URL of the document to
show in the iframe. That is exactly what the getPage() function
returns. I know it getPage() works correctly since I've tried calling
it not in the iframe tag. It seems to me one of two things can be
wrong. One, that html doesn't allow you to call a javascript function
if it's in the context of a value of a property. Or two, there is some
syntax error that I am introducing in the way I am calling the
function. I have tried several variation (i.e. no quotes, single
quotes, etc...), but so far haven't hit upon it.
But thanks for the help, I will try your solution.

Dec 2 '05 #3
Found the answer, it should be this:

<iframe src="javascript :getPage()" .....

Dec 2 '05 #4

ukrbend wrote:
Thanks for the reply. Regarding some of your points, why are the html
comment tags there? They are there in case the user has an older
browser that doesn't support javascript. Several examples of this can
be found here:

http://www.prestwood.com/community/w...ns/beg_js.html
http://www.engin.umd.umich.edu/CIS/c...pt/hellow.html
http://www.webreference.com/js/scripts/basic_date/


The articles you point out are correct. However, some will argue that
there are no longer browsers that do not understand the script tag.

Secondly, I do agree with you. Perhaps what I said was a little harsh.
It's not erroneous, however it is inefficient for you to place html
comment tags there. Consider the following, lets say that you do come
across a browser that doesn't support javascript. What happens then?
Only the areas you have not commented will show up as plain text in the
browser. The better way (as shown in the articles you've given me), is
to place the comment tags around the entire script like so:

<script type = "text/javascript">
<!--
....javascript code...
//-->
</script>

Dec 2 '05 #5

ukrbend wrote:
Found the answer, it should be this:

<iframe src="javascript :getPage()" .....


As for the above, I won't explain what's wrong with the use of the
javascript pseudo protocol. Just simply search the group on
"javascript pseudo protocol" and you'll get many postings about its bad
usage.

http://groups.google.com/group/comp....t=0&scoring=d&

Dec 2 '05 #6
ukrbend wrote:
Thanks for the reply. Regarding some of your points, why are the html
comment tags there? They are there in case the user has an older
browser that doesn't support javascript. Several examples of this can
be found here:

http://www.prestwood.com/community/w...ns/beg_js.html
URL's should be enclosed in <URL: > to help news readers identify them.
It is often recommended to leave a space either side of the URL to
help with copy/paste:

<URL:
http://www.prestwood.com/community/w...ns/beg_js.html
http://www.engin.umd.umich.edu/CIS/c...pt/hellow.html
That link dates from 1997, when script comments were useful.

http://www.webreference.com/js/scripts/basic_date/
You can find thousands of examples of inappropriate and bad programming
with little effort, that doesn't make it recommended best practice. The
answer to your question is given here:

<URL:
http://groups.google.com/group/comp....362c56072adb53
In case you don't currently have access to the web, here's the relevant
quote:

"Your reference is outdated. The `script' element was introduced
along with the `style' element in HTML 3.2 (1997). Versions of HTML
prior to that have been obsoleted by RFC2854 (June 2000), so there
is no need for trying to hide anything. No conforming user agent
will parse this content as PCDATA, especially it must not be
displayed if found within the `head' element. And in contrast of
such "comments" within CSS, they have never been specified for
JS/ECMAScript, hence using them in such script code is potentially
harmful; this includes, but is not restricted to, use in XHTML
(where an XML parser is allowed to remove all markup comments
before building the parse tree)."


Second, you said the src attribute should be the URL of the document to
show in the iframe. That is exactly what the getPage() function
returns.


You discovered how to use of the javascript pseudo-protocol in the src
attribute, but such use is discouraged. Anyone without script support
(because either their UA doesn't support it or they've disabled it) will
see an empty iframe or perhaps get an error message.

One technique is to write the iframe tag using script, so scriptless
browsers will not see the iframe element at all, e.g.

<script type="text/javascript">
document.write(
'<iframe src="somepage.h tml" name="whatever" width="100%"',
' height=600 frameborder="0" scrolling="yes" ><\/iframe>'
);
</script>
You should always quote HTML attribute values. It isn't always
necessary, but it's good practice (it's recommended by in the HTML 4
specification). Where the attribute contains a percentage symbol '%',
it *must* be enclosed in quotes.

<URL: http://www.w3.org/TR/html401/intro/sgmltut.html#h-3.2.2 >
Another method is to use a default scr attribute then modify it with
script so at least scriptless browsers display something useful, even if
it's just a page saying that something would have been displayed but
script support is needed to display it, e.g.:

<iframe src="defaultPag e.html" name="whatever" width="100%"
height="600" frameborder="0" scrolling="yes" ></iframe>

<script type="text/javascript">
var x, y;
if( (x=document.fra mes) && (x=x.whatever) {
if((y=getPage() ){
x.src = y;
}
}
</script>
Untested, but illustrates the method.

[...]
--
Rob
Dec 2 '05 #7
VK

RobG wrote:
One technique is to write the iframe tag using script, so scriptless
browsers will not see the iframe element at all, e.g.

<script type="text/javascript">
document.write(
'<iframe src="somepage.h tml" name="whatever" width="100%"',
' height=600 frameborder="0" scrolling="yes" ><\/iframe>'
);
</script>


For what it worth to mention Netscape 4.x had (has) "JavaScript
entities" one could use for attribute values. Sometimes I'm wondering
why this very useful shortcut for document.write did not go...
So say in the last century :-) the OP's HTML code could look like:

<iframe src="="&{getPag e()};"
name="whatever"
width=100% height=600
frameborder="0" scrolling="yes" ></iframe>

(Standard named entity syntacs &...; and JavaScript statement in curled
braces).
But this is the end of 2005 and no one known browser supports it.

src expects a string and it takes nothing but string. Even if you put
....src="javasc ript:getPage(); "... it doesn't help because the src just
gets this exact combination of letters: "javascript:get Page()" - no
evaluation, no analyzing

Dec 3 '05 #8
VK wrote:
RobG wrote:
One technique is to write the iframe tag using script, so scriptless
browsers will not see the iframe element at all, e.g.

<script type="text/javascript">
document.write(
'<iframe src="somepage.h tml" name="whatever" width="100%"',
' height=600 frameborder="0" scrolling="yes" ><\/iframe>'
);
</script>

For what it worth to mention Netscape 4.x had (has) "JavaScript
entities" one could use for attribute values. Sometimes I'm wondering
why this very useful shortcut for document.write did not go...


I don't see much point in referring to proprietary extensions in dead
browsers. Regardless, the advice would have been the same: depending on
script support to provide essential information for the tag to be useful
is a bad idea.
[...]
src expects a string and it takes nothing but string. Even if you put
...src="javascr ipt:getPage();" ... it doesn't help because the src just
gets this exact combination of letters: "javascript:get Page()" - no
evaluation, no analyzing


Yes, I didn't mention that.

However the OP discovered that regardless of what the HTML 4 spec says
(I suspect it wasn't used as a reference), the javascript
pseudo-protocol 'works' in the iframe src attribute for some browsers.
That it doesn't conform to the HTML 4 specification and that it doesn't
work in some browsers are more good reasons in support of the argument. :-)
--
Rob
Dec 3 '05 #9
Here's another solution, better?

<script>
var pageURL = getPage();
document.write( "<iframe src=\"",pageURL ,"\""," width=100%
height=600 frameborder=\"0 \" scrolling=\"yes \"> ","</iframe>");
</script>

Dec 3 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1743
by: Dennis M. Marks | last post by:
I have a function that displays a list extracted from a table using document.write. In the displayed list I want to have a link that will call the same function but display the list in reverse. Click HERE to reverse A B C When HERE is clicked the page will redisplay as
4
3637
by: nec | last post by:
Hi, I'm having trouble calling a function thats located in a iFrame from the parent. Shortly, i can't figure out the location in DOM. In IE it works fine with a simple line of window.contens.alerter('wassa?'), but it doesnt, of course work in Firefox (0.8). I've tried document.getElementById('contens').alerter('wassa?') and
4
9517
by: coolsti | last post by:
I am aware of the setInterval and setTimeout functions for Javascript, but I can't seem to find an example that lets me do what I need to. I have a script that needs to wait until a certain condition is met. In this case, the condition is that an iframe has been reloaded completely. I do this by examining a hidden document variable in the iframe, and when it has changed from "" to "1" is the signal for me that the iframe is loaded. (if...
9
3289
by: Ragnorack67 | last post by:
.... <div id=work>hello</div> .... <IFRAME id="thisframe" src="./something.htm"></IFRAME> <script>
2
4556
by: Randell D. | last post by:
Folks, I have got this working before, in part with some help from this ng but I never really understood how I got it working... and last time, I was using it via a popup window as opposed to an IFRAME. I've got several months of javascript under my belt and can resolve most things without errors in my Mozilla Javascript Console, but this one just does not do it for me. This is the picture:
5
4643
by: mike | last post by:
If I have a document like: <script> function mike_test() {alert('hi');} </script> <iframe src="blank.html" id="my_iframe1"> </iframe> and in blank.html I have:
4
5323
by: Aaron Gray | last post by:
How do I call a function within an iframe from the container main document ? Many thanks in advance, Aaron
1
1638
by: Hocco | last post by:
Hello Everyone, Please help the novice in java scripting. I'm trying to call changeSection (x, y) function from the iframe. Basically I have my code and my iframe tag in index.html. Then I click on the menu button and it loads an iframe with the name art.html. I have a function that defines which page the user is currently on and writes the name of it. Here what I have in index.html <script type="text/javascript"> function...
0
1508
by: johnw182 | last post by:
Ok, I have a asp.net project. Everything works great. HOWEVER, this project was made to be called from within a frame or iframe. When being used within the iframe the user gets a blank frame when clicking certian buttons. Here is the code for the iframe. <iframe src="http://www.website.com/?cid=A195E1A9-A5D7-4046-89A4-1C00D4F81E35" id="frameName" frameborder="0" width="550" height="750"></iFrame> Here are the steps taken to reproduce.
0
9579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9416
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10199
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10035
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8862
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5436
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3551
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.