admin管理员组

文章数量:1253102

I see it used on websites so I"m unsure. Can I use OnChange and onClick inside my html ? I'm a little confused, I thought javascript was deprecated in html ? However, I think these two actions are part of DOM GlobalEventHandlers ? I think jquery uses it alot so I should be fine ?

<select id="select" class="button" onChange=getValue()>

I think what is meant by not using js in html is

<p javascript:code> </p>

Well, I got an error but I think it has to do with the way chrome extension works. I'll have to look into this:

Refused to execute inline event handler because it violates the following Content Security Policy directive: "script-src 'self' blob: filesystem: chrome-extension-resource:". Either the 'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...') is required to enable inline execution.

I see it used on websites so I"m unsure. Can I use OnChange and onClick inside my html ? I'm a little confused, I thought javascript was deprecated in html ? However, I think these two actions are part of DOM GlobalEventHandlers ? I think jquery uses it alot so I should be fine ?

<select id="select" class="button" onChange=getValue()>

I think what is meant by not using js in html is

<p javascript:code> </p>

Well, I got an error but I think it has to do with the way chrome extension works. I'll have to look into this:

Refused to execute inline event handler because it violates the following Content Security Policy directive: "script-src 'self' blob: filesystem: chrome-extension-resource:". Either the 'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...') is required to enable inline execution.

Share Improve this question asked Jan 29, 2016 at 2:11 ritchieritchie 4551 gold badge4 silver badges13 bronze badges 3
  • Sorry, I'm a little confused as to what your exact question is. Do you think you could clarify? Are you asking about Chrome extensions? – BurningLights Commented Jan 29, 2016 at 2:15
  • 1 @BurningLights I was asking if I could use onClick and onChange in my html or is it deprecated ? – ritchie Commented Jan 29, 2016 at 2:21
  • 2 According to MDN (developer.mozilla/en-US/docs/Web/Guide/HTML/…) event attributes should be avoided. But, I couldn't find anything about them being deprecated. – BurningLights Commented Jan 29, 2016 at 2:50
Add a ment  | 

2 Answers 2

Reset to default 10

Sounds like you're writing a Google Chrome extension. You cannot use any inline event handlers (i.e, onEvent attributes) under the security model used for extension pages*. All events must be set up from Javascript code, e.g.

document.getElementById("select").addEventListener("change", getValue);

or, if you're using jQuery:

$("#select").change(getValue);

*: Specifically, the Content Security policy for Chrome extensions.

You can use onclick in html and you have to write a javascript function that onclick will do. Using onclick does not required jQuery and you can just do pure javascript.

HTML

<button onclick="foo()">Do something</button>
<p id="foo"></p>

JS

function foo() {
    document.getElementById("foo").innerHTML = "Foo";
}

本文标签: javascriptIs onChange or onClick deprecated in html GlobalEventHandlersStack Overflow