jquery in included javascript file
I am new to jquery and javascript so I haven't been able to figure this problem out; nor have my google-fu skills helping me today.
I developed a page with HTML and Javascript/Jquery, placing all the jquery between s开发者_如何学Pythoncript tags and all was well. When I was done the page I realized that I wanted the same javascript on another page so I put all that jquery/javascript into an external file and tried to include it.
That's where things went downhill. The javascript doesn't work at all on the initial page. Nothing happens. I checked the error console and I am not getting any JS errors.
Why does this not work and how do I get it to work?
This is the JS code in the external file: http://pastebin.com/wHBmbHLs
The problem probably has little to do with your Javascript since you're not even getting any errors. It likely has everything to do with the manner in which you're including the script. Does your script
tag look like this:
<script type="text/javascript" src="your source"></script>
Or perhaps more to the point...we need to see your html in order to begin to help you here.
For reference, in the future if you have a problem like this, try to isolate the problem. For example, create a small Javascript file that contains one line:
alert('hello');
Then include it. If it fails to run, you know the error is in your HTML, and not in your Javascript.
Difficult to tell without seeing the HTML, but it is probably that you simply have the wrong script tag (as Surreal points out for script to work in IE is has to be type "text/javascript", even though this is a depreciated option. The newer "application/javascript" won't work in IE7 or IE8 as far as I know)
You might also want to check the Javascript itself, you have a few funny things going on. For example you have a function name "postBox" in which you define a variable "postBox". Don't know if this is legal Javascript but even if it is that doesn't mean an interpreter won't get confused.
Also use jQuery to make dynamic tags. Use something like
var newDiv = jQuery(""); var newInput = jQuery(""); newInput.attr("type", "text"); newInput.css({"width": "500px"}; newDiv.append(newInput);
Takes up more space but this is less error prone and can give better errors when it goes wrong. Browsers like IE8 will fail silently if you try to insert invalid HTML from a string using jQuery, this can be very frustrating as you have no idea why it didn't work.
精彩评论