473,657 Members | 2,422 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 "automatica lly" ?)

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

Any good ideas?
Jun 12 '07 #1
2 1269
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 "automatica lly" ?)

(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
1937
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" xmlns:xalan="http://xml.apache.org/xalan" xmlns:my-ext="ext1" extension-element-prefixes="my-ext"> <!--The component and its script are in the xalan namespace and define the
13
9426
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 intercept the key so that I can do stuff on the server side to make the new window behave correctly? (We have a JSP-based webapp which stores state in the session. Now if two windows access (and modify!) the same session, then madness will result....
8
3019
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 shows the minutes and seconds remaining before submission. We have a fairly tight deadline (by 5PM EST, Friday, June 25); we are using a "bounty" in the hope of getting a few good responses in a hurry. BOUNTY: $200 for first place, plus a $50...
136
9306
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 code was littered with document.all and eval, for example, and I wanted to create a practical list of best practices that they could easily put to use. The above URL is version 1.0 (draft) that resulted. IMO, it is not a replacement for the FAQ,...
104
16929
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 that array Could you show me a little example how to do this? Thanks.
7
2084
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 says to execute in a second, that is really a second after the entire call chain is finished. So if the call chain happens to take 10 seconds to complete - the setTimeout method will execute in 11 seconds. Timers just execute at regular...
1
2334
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 is HTML with Javascript. The Javascript on the page instantiates an ActiveX control written in C++ ATL. The ActiveX does not have a visual interface and its basic functionality is to access the serial port and do some calculations and return the...
2
1212
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 create a instance of the component. The purpose of the component is collect the printers installed on the users machine and return the values to javascript. This works fine on some machines and does not work on few machines. Javascript is...
11
6755
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. But, my problem is on the client side. I want to have a program, or a web page containing buttons that, when pressed sends a signal to the TCP/IP component and waits for a response back. Maybe this seems strange, but let's say I have a...
2
9031
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 figure this problem out. Any suggestions? thanks Joel
0
8411
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8323
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8838
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8513
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7351
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6176
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4173
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.