473,503 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Javascript component

4 New Member
If I write a javascript object that needs an inclusion of a .css file in order to be displayed nicely on the webpage...

...how can I link the .css file into the html page only by including the .js file? (so that the user only includes the .js file and the rest goes "automatically" ?)

(suppose the path to the css is the same as for the .js)

Any good ideas?
Jun 12 '07 #1
2 1262
dmjpro
2,476 Top Contributor
If I write a javascript object that needs an inclusion of a .css file in order to be displayed nicely on the webpage...

...how can I link the .css file into the html page only by including the .js file? (so that the user only includes the .js file and the rest goes "automatically" ?)

(suppose the path to the css is the same as for the .js)

Any good ideas?

look at this example .....

Expand|Select|Wrap|Line Numbers
  1. var css_link = document.createElement('LINK');
  2. css_link.setAttribute('rel','stylesheet');
  3. css_link.setAttribute('href','css_file_name');
  4. document.body.appendChild(css_link); //This line i have confusion
  5.  
try this ..... i think it ll work
have a good day

kind regards,
dmjpro.
Jun 12 '07 #2
mrhoo
428 Contributor
link elements belong in the head.
this method firsts checks to see if the link is already loaded.
If it is, it moves it to the sharp end of the cascade,
otherwise it creates a new link element.

Expand|Select|Wrap|Line Numbers
  1. window.addLink= function(url,t){
  2.     var A= document.getElementsByTagName('link');
  3.     var L= A.length, el;
  4.     for(var i= 0; i< L; i++){
  5.         if(A[i].href==url){
  6.             el= A[i];
  7.             break;
  8.         }
  9.     }
  10.     if(!el){
  11.         el=document.createElement('link');
  12.         el.rel="stylesheet";
  13.         el.type="text/css";
  14.         el.media="screen";
  15.         el.href=url;
  16.         if(t) el.title= t;
  17.     }
  18.     return document.getElementsByTagName('head')[0].appendChild(el);
  19. }
Jun 12 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1925
by: Frank | last post by:
Hey all, I can't seem to get javascript running in my XSL document. <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"...
13
9396
by: Kai Grossjohann | last post by:
It seems that Ctrl-N in Mozilla opens a new empty browser window. That's fine, I don't need to do anything about it. But Ctrl-N in IE appears to clone the current window. Is there a way to...
8
2989
by: Prometheus Research | last post by:
http://newyork.craigslist.org/eng/34043771.html We need a JavaScript component which will auto-submit a form after a set period has elapsed. The component must display a counter that dynamically...
136
9201
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
104
16852
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
7
2073
by: Marina Levit [MVP] | last post by:
Does anyone knows if this exists? Basically, a way to tell a method to execute, but a certain amount of time after the current event chain is finished - not immediately. With setTimeout, even if it...
1
2329
by: Hitchkas | last post by:
I posted this in PocketPC newsgroup with no response yet, hopefully somebody here has an answer. I have an application that runs locally on PocketPC. The user interface for this application...
2
1203
by: L Gopi | last post by:
Hi! I have created a com component using framework 1.1. This component will be installed on the users machine. This component will be used by my 2.0 web application. A javascript call is made to...
11
6740
by: test | last post by:
Hi, I'm very new to Javascript so maybe my question could be stupid. I'm playing with microcontrollers and a TCP/IP component. This means that I can control electronics via TCP/IP, UDP and so on....
2
9004
by: joelkeepup | last post by:
Hi, I made a change this morning and now im getting an error that says either "a is undefined or null" or "e is undefined or null" the microsoft ajax line is below, I have no idea how to...
0
7353
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7011
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5596
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4689
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3170
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.