/    Sign up×
Community /Pin to ProfileBookmark

Executing Javascript inside JavaScript.

Hi All,

I have a situation to execute a javascript dynamically by calling a function. The script is some what like this

<script src=”http://www.surveymonkey.com/jsPop.aspx?sm=jkjkjkjfddsafkjsfaldflsdfasdf”></script>.

If we paste this javascript inside the body of a webpage a survey window will popup when the web page is loaded. But I want to call this script inside a javascript function like

function callSurvey(){
//Execute the script on function call.
}

and trigger this function during unload event.

Is this feasible to execute javascript inside javascript functions. If so can any one provide some sample code snippets. Thanks in advance

Regards
Jacob

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@MikeOSJun 30.2008 — I'm not 100% certain what your problem is but if you want to call the script during a page unload just use the onunload event in the body tag like so:

...

function callSurvey(){

//Execute the script on function call.

}

</script>

</head>

<body onunload="callSurvey()">

...

Hope this is what you mean but if not can you clarify?

Thanks
Copy linkTweet thisAlerts:
@Declan1991Jun 30.2008 — You will probably have to write the script to the page, if you want to be able to call it. Something like:<i>
</i>function callSurvey(){
var s = document.createElement("script");
s.type="text/javascript";
s.src="http://www.surveymonkey.com/jsPop.aspx?sm=jkjkjkjfddsafkjsfaldflsdfasdf";
document.getElementsByTagName("head")[0].appendChild(s);
}
I'm not 100% certain will that work cross-browser, I just cannot think of the bug that relates to script elements at the moment.
Copy linkTweet thisAlerts:
@jacob_ceaauthorJul 01.2008 — Hi Declan,

It worked for me. THanks.

Jacob
×

Success!

Help @jacob_cea 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 6.18,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...