Skip to content

Instantly share code, notes, and snippets.

@nathanhammond nathanhammond/attrbug.html Secret

Created Jan 18, 2011
Embed
What would you like to do?
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
You can’t perform that action at this time.