View Full Version : Simple Javascript Question

05-13-2009, 05:00 PM
Hey everybody, i decieded to start learning Javascript.

I got a problem, im making a script that allows me to add text into a text box. I have a button that when clicked accesses the javascript from a different file. But when i click the button nothing happens, so i copy the script into the actual form post and it works. So im wondering why it wont work.

Heres my HTML :

<script src="./includes/editor.js"></script>
<table width="560" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<form id="form1" name="form1" method="post">
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<td colspan="4" bgcolor="#E6E6E6"><strong>Create New Topic</strong> </td>
<td colspan="4" bgcolor="#C6C6C6">
<input type="button" class="button" value="Bold" name="bold" onclick="wrapText('[b ]', '[ /b]')" onMouseOver="helpline('bold')" />

Then in my editor.js file i have :

function wrapText(openTag,closeTag){

var workArea = document.getElementById('detail');
!workArea.focus() ? workArea.focus() : null;
if (!document.selection)
var workText = workArea.value.substring(workArea.selectionStart,workArea.selectionEnd);
if (workText != "")
workArea.value = workArea.value.replace(workText,openTag+workText+closeTag);
workArea.selectionStart = "";
workArea.selectionEnd = "";
else {
var workText = document.selection.createRange();
if (workText.text != "")
workText.text = openTag+workText.text+closeTag;

As i said if i copy this into my HTML file it works, but if it stays in the editor file nothing works.

05-13-2009, 05:14 PM
Usually it's simple: if embedding it in your HTML works, but loading it from a separate file doesn't work, there's a problem with the filepath, or perhaps you haven't put pure js (no html) in that file. Put an alert('ok') right at the beginning of editor.js; if it doesn't pop up, something's amiss.

Not sure what this means:

I have a button that when clicked accesses the javascript from a different file.

05-13-2009, 05:41 PM
Nothing, but i have other functions in that .js file and they work fine. But this one function only works if i have the javascript in the actual HTML file.

05-13-2009, 05:56 PM
Where is 'helpline' defined? Don't see it.
Again - nothing mysterious here. If the JS is written directly in the html file, between script tags - don't forget to add type="text/javascript" to all script tags - it gets handled the same way as JS stored seperately and downloaded via the src attribute. Something else is wrong.

05-13-2009, 06:02 PM
I have other function in my editor.js file as i said, they work fine, just this one function.

I added the type="text/javascript" and still no difference. So im confused why it wont work.

EDIT: Ok i tried changing the path to ./editor.js and moving the editor to that path in my directory and it works, which is odd. When i move it back and change the src="./includes/editor.js" it no longer works.

Can anyone think of a reason why?