Side navigation
#8130 closed bug (invalid)
Opened February 01, 2011 06:26PM UTC
Closed February 01, 2011 07:28PM UTC
$.get incorrectly executes javascript files
| Reported by: | dennydaugherty | Owned by: | |
|---|---|---|---|
| Priority: | low | Milestone: | 1.next |
| Component: | ajax | Version: | 1.5 |
| Keywords: | Cc: | ||
| Blocked by: | Blocking: |
Description
According to the documentation $.get() will load the contents of the specified file, however if you try and $.get() a javascript file it will execute it. The expectation is that $.get() loads the contents of the file whereas only $.getScript() execute them.
I created the following HTML and Javascript files to test. Note the file being loaded with $.get() has an alert() in it that is incorrectly executed. Though everything else works as expected, the test.js file should be executed.
test.html
<!DOCTYPE html>
<html>
<head>
<title>Testing jQuery.get() with Javascript Files</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.5.min.js"></script>
<script type="text/javascript">
console.log('Loading test.js using $.get()');
$.get('test.js', function (data) {
console.log('Finished loading test.js');
$('#test').html(data);
});
</script>
</head>
<body>
<code id="test">
</code>
</body>
</html>
test.js
alert('Hello world');
Attachments (0)
Change History (1)
Changed February 01, 2011 07:28PM UTC by comment:1
| component: | unfiled → ajax |
|---|---|
| priority: | undecided → low |
| resolution: | → invalid |
| status: | new → closed |
Thanks for taking the time to contribute to the jQuery project by writing a bug report.
After checking your bug report I came to the conclusion that this isn't a bug. If you read the jQuery.get() documentation directly at the beginning it says
jQuery.get( ... )is a short hand forjQuery.ajax({ ... }).If you read up on the
dataTypeparameter in the jQuery.ajax() documentation it says:This applies directly to your report. You didn't specify the
dataTypeproperty so jQuery guessed by the respones content-type that this is a javascript file, and thus executes it as described by the documentation. If you don't want that to happen you can set thedataTypeparameter ofjQuery.get()to "text".