/    Sign up×
Community /Pin to ProfileBookmark

Making a javascript function

I need to convert this code into 1 function so that if I send it a URL, it returns the tinyURL. I’m using it in a Greasemonkey script, so the code below as-is will not work with the onload parameters.

[CODE]
<script type=”text/javascript” charset=”utf-8″>
window.onload = function() {
var s = document.createElement(‘script’);
var v = document.getElementById(‘url’).value;
s.setAttribute(‘src’, ‘http://remysharp.com/tinyurlapi?callback=tinyurlCallback&url=’ + v);
document.body.appendChild(s);
}

function tinyurlCallback(url) {
alert(‘URL converted to: ‘ + url);
}
</script>[/CODE]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991Mar 02.2008 — [code=php]function addScript() {
var s = document.createElement("script");
s.setAttribute("src","http://remysharp.com/tinyurlapi?callback=tinyurlCallback&url=" + document.getElementById('url').value);
s.setAttribute("type","text/javascript");
document.appendChild(s);
}[/code]

What else do you want it to do?
Copy linkTweet thisAlerts:
@parterburnauthorMar 02.2008 — I think what I"m looking for is something like a stand-alone function like this:

[CODE]function addScript(url) {
var s = document.createElement("script");
s.setAttribute("src","http://remysharp.com/tinyurlapi?callback=tinyurlCallback&url=" + url);
s.setAttribute("type","text/javascript");

return new_url;
}[/CODE]


I need to be able to call that and assign it to a variable:

new_url = addScript("http://www.google.com");

new_url would then be assigned the value of "http://tinyurl.com/2tx".
×

Success!

Help @parterburn 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.19,
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,
)...