/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Another ‘Can I …’ question

Can I have a method to display the code of a JS function without saving the code in another text file and using ajax to read it?

I can envision saving an external JS file as TXT, but now I have two files maintain.
I want to display the code of the external file from the JS code itself.

Can I do this?

Alternatively, if the function is in the <HEAD> tag, can I display it within a <textarea> element?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@TheBearMayJun 22.2009 — Not sure how well it will validate, but:

[code=html]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript" id="myscript" >
if(window.attachEvent)
window.attachEvent("onload", showVal);
else
window.addEventListener("load", showVal, false);

function showVal() {
document.getElementById("oPut").innerHTML = document.getElementById("myscript").innerHTML;
}
</script>
</head>

<body>
<div>
<textarea id="oPut" style="width:80&#37;;height:10em"></textarea>
</div>
</body>
</html>[/code]
Copy linkTweet thisAlerts:
@svidgenJun 22.2009 — Sure.
[code=html]<html>
<head>
<script type='text/javascript'>
function loadCode() {
// test comment
document.getElementById('test').value = loadCode.toString();
}
</script>
</head>

<body>
<textarea id='test'>Your function code will be loaded here.</textarea>
<br /><input type='button' onClick='loadCode();' value='Load' />
</body>
</html>[/code]

IE7, FF3, and Chrome all display the function. IE7 and Chrome even show the comment.
Copy linkTweet thisAlerts:
@Sterling_IsfineJun 22.2009 — Can I have a method to display the code of a JS function without saving the code in another text file and using ajax to read it?
[/QUOTE]
[CODE]alert( functionName.toString() );[/CODE]
Copy linkTweet thisAlerts:
@JMRKERauthorJun 22.2009 — Wow :eek: ... I'll give them all a try.

Thanks for the information. ?
×

Success!

Help @JMRKER 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 4.29,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

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