Insert JavaScript in CSS

Source: Internet
Author: User
Tags insert modify
css|javascript| Insert

The creator of the Quirksmode site found an interesting trick. The Background-image property in CSS can be inserted into JavaScript. Unfortunately, only IE5/6 support is currently available. (IE7 I have no condition to test)

Run Code Box

<style type= "Text/css" > #test {border:1px solid #000000; padding:10px; Background-image:url (' Javascript:alert (" Did you see me? ")} </style><div id= "Test" > Display message Oh </div>

[Ctrl + A ALL SELECT hint: You can modify some of the code, and then run]

If we insert JavaScript into many different ID selectors in the CSS. The ID selector is only invoked in the preceding XHTML. The JavaScript code is not run. Instead of calling the ID selector, where the code does not run.

As follows, we have defined two ID selectors. Test1 and Test2. However, the test2 is not invoked in XHTML. So you can still only see the JavaScript code in test1.

Run Code Box

<style type= "Text/css" > #test1 {border:1px solid #000000; padding:10px; Background-image:url (' Javascript:alert ( "Have you seen me?"); #test2 {border:1px solid #000000; padding:10px background-image:url (' You can't see me! ') </style><div id= "Test1" > Display message Oh </div>

[Ctrl + A ALL SELECT hint: You can modify some of the code, and then run]

In addition to displaying messages, the author attempts to use this technique to generate random background images.
Background-image:url (Javascript:document.write (' pix/test ' + (parseint (Math.random () *5) +1) + '. gif ');

Unfortunately, after many attempts, the idea was ultimately considered impossible. I tried it myself. It doesn't produce a random background picture. It's just something that shows the background. And the browser continues to explain the rest of the running XHTML.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.