/    Sign up×
Community /Pin to ProfileBookmark

Calling an external javascript from within a script

I know that you can call a php script using javascript. For example:

[code=html]
<script type=”text/javascript” src=”myscript.php”></script>
[/code]

I need to call an external script from within a javascript function.

[code=html]
<script type=”text/javascript”>
function rotateimages(){
document.getElementById(“slideshow”).setAttribute(“src”, <!– call script –>)
}

window.onload=function(){
setInterval(“rotateimages()”, 5000)
}
</script>

So how do I call the external script there?
[/code]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@DeveloperBillNov 02.2011 — Unless I am really missing something, I don't see why you would refer to a php script with type="text/javascript".

But for your answer, you would want your external script to contain another function that returns a string that you want to set the "src" to. So in your code:
[CODE]document.getElementById("slideshow").setAttribute("src", setSrc(arguments,...));
[/CODE]

Arguments of course depend on your needs. If none, just write setSrc() with no arguments.

Then just code your setSrc function, ending with a "return" statement with the src attribute to be set.

The final step is to save your function as a text file with type "js" for example setsrc.js. Then in the <head> section of your code, just include this line:
[CODE]<script type="text/javascript" src="setsrc.js"> [/CODE]

Hope that's what you are looking for!
Copy linkTweet thisAlerts:
@007JulienNov 03.2011 — A javascript file can always be built with PHP (*), like here. See, for example, this page

(*) Like this very simple script :
[code=php]<?php
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Content-Type:text/javascript;charset=utf-8");

if (!empty($_GET)) {
// lecture du GET et execution des t&#226;ches
}
exit("alert('Votre adresseIP est : ".$_SERVER['REMOTE_ADDR']."');");
?>[/code]

This method can be useful...
×

Success!

Help @mrgrammar spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.13,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...