aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes calling function from src attribute of html tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "calling function from src attribute of html tag" Watch "calling function from src attribute of html tag" New topic
Author

calling function from src attribute of html tag

Bloo Barton
Ranch Hand

Joined: May 09, 2005
Posts: 63
I want to be able to get my string for the src attribute from a Javascript function. I know there is a way to do this, but it has been a few years since I last did this and I can't seem to get the syntax right or something...

Currently I have a function like this...

<script language="javascript">
function getUrl(){
/* build URL here ... */
return URL;
}
</script>

Then I try to use the function like this...

<iframe src=javascript:getUrl(); width="100%" height="100%"></iframe>

This does not work though...
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6

<script type="text/javascript">
window.onload = function(){
URL = "http://www.pascarello.com";
document.getElementById("iframe1").src=URL;
}
</script>

<iframe id="iframe1" src="" width="100%" height="100%"></iframe>


Eric
Bloo Barton
Ranch Hand

Joined: May 09, 2005
Posts: 63
Eric,

That method looks like it is pushing the value from the function out to the tag. I'm looking for something more re-usable. I want to be able to call the function from the tag, not push the data into the tag when the page loads.
Yuriy Zilbergleyt
Ranch Hand

Joined: Dec 13, 2004
Posts: 429
I'm not sure, would this work?

<script>
function getFrame1(iframe) {
var src = "http://example.com/frame1";
if (!iframe.hasBeenSet) {
iframe.src = src;
iframe.hasBeenSet = true;
}
}
</script>
.
.
.
<iframe src="" onload="getFrame1(this)" width="100%" height="100%" ></iframe>

-Yuriy
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Yuriy,

I guess I was not awake on trying to figure out a way to stop the infinite loop of loading! Soon as I saw your post I said: "DUH!". I do stuff similiar on other projects of mine. LOL

Eric
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: calling function from src attribute of html tag