Bug Tracker

Opened 8 years ago

Closed 8 years ago

#8130 closed bug (invalid)

$.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');

Change History (1)

comment:1 Changed 8 years ago by jitter

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".

Note: See TracTickets for help on using tickets.