Skip to main content

Bug Tracker

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 jitter comment:1

component: unfiledajax
priority: undecidedlow
resolution: → invalid
status: newclosed

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 for jQuery.ajax({ ... }).

If you read up on the dataType parameter in the jQuery.ajax() documentation it says:

dataType: The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response. [...] in 1.4 script will execute the script [...]

This applies directly to your report. You didn't specify the dataType property 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 the dataType parameter of jQuery.get() to "text".