jquery - how to isolate CSS in a website gadget? -
this question has answer here:
i have livescore website , trying make gadget of website show part of in others websites,the problem face how isolate code (javascript , css) code of host site gadget. use "anonymous function" this:
(function() { // localize jquery variable var jquery; /******** load jquery if not present *********/ if (window.jquery === undefined || window.jquery.fn.jquery !== '1.8.3') { var script_tag = document.createelement('script'); script_tag.setattribute("type","text/javascript"); script_tag.setattribute("src", "http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"); if (script_tag.readystate) { script_tag.onreadystatechange = function () { // old versions of ie if (this.readystate == 'complete' || this.readystate == 'loaded') { scriptloadhandler(); } }; } else { // other browsers script_tag.onload = scriptloadhandler; } // try find head, otherwise default documentelement (document.getelementsbytagname("head")[0] || document.documentelement).appendchild(script_tag); } else { // jquery version on window 1 want use jquery = window.jquery; main(); } /******** called once jquery has loaded ******/ function scriptloadhandler() { // restore $ , window.jquery previous values , store // new jquery in our local jquery variable jquery = window.jquery.noconflict(true); // call our main function main(); } /******** our main function ********/ function main() { jquery(document).ready(function($) { /******* load css *******/ var css_link = $("<link>", { rel: "stylesheet", type: "text/css", href: "http://localhost/aaa/widget/widget.css" }); css_link.appendto('head'); var txt; txt='<div id="wrapper">'; txt=txt+'<ul class="tabs">'; txt=txt+'<li id="fixtures_tab"><a href="#fixtures">all</a></li>'; txt=txt+'<li id="live_tab"><a href="#live">live</a></li>'; txt=txt+'<li id="finished_tab"><a href="#finished">finished</a></li>'; txt=txt+'<li id="program_tab"><a href="#program">program</a></li>'; txt=txt+'<li id="postpond_tab"><a href="#postpond">postpond</a></li>'; txt=txt+'<li id="selected_tab"><a id="f" href="#fav">selected (0)</a></li>'; txt=txt+'</ul>'; txt=txt+'<div class="tab-container">'; txt=txt+'<div id="fixtures" class="tab-content"><script type="text/javascript">get_all_today_matches();</script></div>'; txt=txt+'<div id="live" class="tab-content"><script type="text/javascript"></script></div>'; txt=txt+'<div id="finished" class="tab-content"><script type="text/javascript"></script></div>'; txt=txt+'<div id="program" class="tab-content"><script type="text/javascript"></script></div>'; txt=txt+'<div id="postpond" class="tab-content"><script type="text/javascript"></script></div>'; txt=txt+'<div id="fav" class="tab-content"><script type="text/javascript"></script></div>'; txt=txt+'</div>'; txt=txt+'</div>'; $('#widget-container').html(txt); }); } })(); // call our anonymous function
everything going fine jquery not css (the css file loaded , worked fine of hosting site css effecting css) have no control on hosting site css , think have isolated code using anonymous function why css affected website orginal css ??? me please have been @ point while
i know of 2 possible solutions.
one have code embedded on other site via iframe
. css won't clash then, unless other site specifically writes something modify css inside iframe. default, won't problem.
otherwise, you'll have add mywidget_
beginning of of class/id names prevent collision other site.
Comments
Post a Comment