Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Inline Javascript as a Tag Attribute
Old 10-20-2011, 11:49 AM Inline Javascript as a Tag Attribute
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
I am trying to find out if the following is the correct usage and is valid across all types of browsers. I have tried the html entity converted method in IE8 and the JavaScript executed perfectly without any errors.

One way to embed inline JavaScript in an HTML tag would be to use literal charactures, but this is considered invalid xhtml markup.
HTML Code:
<button onclick='alert("1 > 0 && 0 < 1 = "+(1 > 0 && 0 < 1 ? "true" : "false"));'>Test Me 1</button>
When I use html entities for certain charactures, this works in IE8 and the JavaScript still works.
HTML Code:
<button onclick="alert(&quot;1 &gt; 0 &amp;&amp; 0 &lt; 1 = &quot;+(1 &gt; 0 &amp;&amp; 0 &lt; 1 ? &quot;true&quot; : &quot;false&quot;));">Test Me 2</button>
Will the second attempt work across all browsers?
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-23-2011, 08:48 AM Re: Inline Javascript as a Tag Attribute
Physicsguy's Avatar
404 - Title not found

Posts: 919
Name: Scott Kaye
Location: Ontario
Trades: 0
You could escape the quotes with \.

Code:
<button onclick="alert(\"1 > 0 && 0 < 1 = \"+(1 > 0 && 0 < 1 ? \"true\" : \"false\"));">Test Me 1</button>
Single quotes would also work here.

EDIT: It's not working as expected... Can you do that kind of thing using JS? Try just an if statement.

EDIT2: This works for me:
Code:
<button onclick="if(1>0){o='true';}else{o='false';}alert(o);">Test Me 1</button>
But I'm not sure if that's what you're going for.
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!

Last edited by Physicsguy; 10-23-2011 at 08:54 AM..
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 10-24-2011, 09:33 AM Re: Inline Javascript as a Tag Attribute
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
I would think escaping quotes would not work in html markup and it would break. Also you have other illegal chars such as < and >

<edit>
I'm not looking to replace the js code, but seeing if the method is correct and compatable accross browsers
</edit>
__________________

<mgraphic /> - I don't have a solution but I admire the problem.

Last edited by mgraphic; 10-24-2011 at 09:54 AM.. Reason: Added new comment
mgraphic is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Inline Javascript as a Tag Attribute
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.53868 seconds with 12 queries