Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Mar 2009
    Location
    United Kingdom
    Posts
    161
    Thanks
    6
    Thanked 28 Times in 28 Posts

    Simple Javascript Question

    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 :
    Code:
    <script src="./includes/editor.js"></script>
    <table width="560" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
    <tr>
    <form id="form1" name="form1" method="post">
    <td>
    <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
    <tr>
    <td colspan="4" bgcolor="#E6E6E6"><strong>Create New Topic</strong> </td>
    </tr>
    <tr>
    <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 :

    Code:
    	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;
    				 document.selection.empty();
    				}
    			}
    	}
    As i said if i copy this into my HTML file it works, but if it stays in the editor file nothing works.

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Mar 2009
    Location
    United Kingdom
    Posts
    161
    Thanks
    6
    Thanked 28 Times in 28 Posts
    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.

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    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.

  • #5
    Regular Coder
    Join Date
    Mar 2009
    Location
    United Kingdom
    Posts
    161
    Thanks
    6
    Thanked 28 Times in 28 Posts
    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?
    Last edited by Killermud; 05-13-2009 at 07:17 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •