Home >Web Front-end >Front-end Q&A >Can javascript be placed in css?

Can javascript be placed in css?

藏色散人
藏色散人Original
2021-06-26 10:27:482051browse

Javascript cannot be placed in css because it requires relatively high system resources, so it is best not to write js code in css. It is necessary to separate js code and css code, which is easier to maintain.

Can javascript be placed in css?

The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.

Can javascript be placed in css? Questions about writing javascript code in css?

I saw a piece of code in an article before and felt a little confused?

The code I probably remember from that article is as follows:

<style type="text/css">
 
body{
 
background:url(javascript:alert("test!"));
 
}
 
</style>

According to what was said in that article, the js code in the url in the css will be executed! In this case, can I understand that js code can be written in the URL in css, so I did the following experiment, and my code is as follows:

<script type="text/javascript">
 
function getImgPath() {
alert("hi 江西省赣州市于都县渡江大道XXXX号");
return "img/004.gif";
}
</script>
 
<style type="text/css">
 
/* 在css中的url中写javaScript代码 */
background-image: url(javascript:getImgPath());
/* background-image: url("img/1.jpg"); */
 
</style>

Then I opened the jsp page in Firefox and found that the getImgPath() function in javaScript was not executed, the sentence "hi No. XXXX, Dujiang Avenue, Yudu County, Ganzhou City, Jiangxi Province" was not printed, and the web page was not set. Background image, so putting javaScript code in the url in css will not execute the js code? With doubts, I tested it again in the IE browser, and it still couldn't execute the js code in the url in the css. The version of the IE browser I used was IE11, and the version of Firefox was 49.0. Could it be that only in Will the js code in the url in css be executed only in lower version browsers?

Then again, because I saw some people on the Internet saying that there is no need to worry about this thing. It is unique to IE and is not very versatile. It is recommended not to use it because it requires a lot of system resources. This article mentions this!

yes! It is best not to write js code in css. It is best to separate js code and css code, which is easier to maintain and can be clearly distinguished!

If you know this knowledge, you can leave a message Give it to me, because I am not sure under what circumstances the js code in css will be executed, and I am not sure whether the js code in css will be executed!

[Recommended: javascript advanced tutorial]

The above is the detailed content of Can javascript be placed in css?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn