Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using <script> in CSS

Is there any way to write script in css and call or execute it whenever required ?

I need a <script> tag to be executed .

i need something like this..

css code

#execute{

<script> ..some script.. </script>

}

so whenever i use

<html>

.
.
.
.<div id="execute" />
.
.
.
.
</html>

so if i change the script changes will be reflected everywhere.

Is it possible?

EDIT:

Is it possible to keep my <script></script> tags inside some js file and i will host it. and then i will call some function() from my HTML so that the script will be executed everywhere i need it.

Can someone show me any example, tutorial how i can do it. I don't have much information about the Js file and how the function should be called.

Thank you all

like image 779
Sangram Nandkhile Avatar asked Jan 19 '23 07:01

Sangram Nandkhile


2 Answers

Does it have to be in CSS? jQuery is a great, simple way to do what you're asking. You put all your style information in the CSS (what it's intended for) and keep your javascript in the html or a .js file. Take a look at http://jquery.com. The code would look something like this

$(function() {
    $('#execute')
        .someCoolFunction()
        .anotherCoolFunction();
});

You use $(function() { /* code */ }); to run the code when your document is ready, and you use $('#execute') to grab the element with the execute tag. You can then do a lot of cool javascript really easily with that jQuery element.

like image 149
redbmk Avatar answered Jan 23 '23 05:01

redbmk


No, you cannot mix CSS and Javascript this way. Why would you want to?

If you simply want a common JavaScript include, do it like this:

<script type="text/javascript" src="yourscript.js"></script>
like image 25
Brad Avatar answered Jan 23 '23 03:01

Brad