jihn
jihn

Reputation: 51

Reason to include javascript with document.write

I started working on a project a came across a strange way to include a CSS file using javascript.

Example from the project:

document.write('<link rel="stylesheet" type="text/css" href="css/plugins/jquery.datatables.css" />');

Is there any reason (advantage) to include the file this way?

Upvotes: 3

Views: 66

Answers (3)

C.Champagne
C.Champagne

Reputation: 5489

This code does not include JavaScript but the CSS include...by using JavaScript. It allows to dynamically select your CSS maybe to select themes or adapt layout to the device.

Upvotes: 2

Aynolor
Aynolor

Reputation: 413

Maybe to load the stylesheet only when dataTable is displayed and avoid unnecessary request ?

Upvotes: 0

j08691
j08691

Reputation: 207923

The CSS file will only be added to the page if the user has JavaScript enabled. Bandwidth saver.

Upvotes: 4

Related Questions