Skip to content

Instantly share code, notes, and snippets.

@nathanhammond
Created January 18, 2011 04:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathanhammond/a1defd74d3710ef82a4e to your computer and use it in GitHub Desktop.
Save nathanhammond/a1defd74d3710ef82a4e to your computer and use it in GitHub Desktop.
Safari $('script').attr('event'); Bug
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Safari $('script').attr('event'); Bug</title>
<script src="http://code.jquery.com/jquery-git.js" type="text/javascript"></script>
<script id="myscript" event="eventattributecontent" src="https://ajax.aspnetcdn.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js" type="text/javascript"></script>
</head>
<body>
<h1>Safari $('script').attr('event'); Bug</h1>
<p>This bug occurs when trying to use the jQuery .attr() on a script tag to access an attribute named "event". This page tests that scenario.</p>
<h2>Script Being Included</h2>
&lt;script id="myscript" event="eventattributecontent" src="https://ajax.aspnetcdn.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js" type="text/javascript"&gt;&lt;/script&gt;
<h2>Test Code</h2>
<pre>
&lt;script type="text/javascript"&gt;
$(document).ready(function() {
// DOM Method
var dom = document.getElementById('myscript').getAttribute('event');
console.log('DOM: "' + dom + '"');
// Direct attribute access.
var direct = document.getElementById('myscript').event;
console.log('Direct: "' + direct + '"');
// jQuery .attr()
var jqueryattr = $('#myscript').attr('event');
console.log('jQuery .attr(): "' + jqueryattr + '"');
});
&lt;/script&gt;
</pre>
<h2>Results</h2>
<p>The expected result is the string "eventattributecontent". The actual result is an empty string. Occurs in the latest version (from http://code.jquery.com/jquery-git.js).</p>
<script type="text/javascript">
$(document).ready(function() {
// DOM Method
var dom = document.getElementById('myscript').getAttribute('event');
console.log('DOM: "' + dom + '"');
// Direct attribute access.
var direct = document.getElementById('myscript').event;
console.log('Direct: "' + direct + '"');
// jQuery .attr()
var jqueryattr = $('#myscript').attr('event');
console.log('jQuery .attr(): "' + jqueryattr + '"');
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment