Is there any way to load css and javascript from a string?

2024/2/27 7:42:38

I've seen many examples of loading CSS and javascript dynamically from a file. Here's a good example. But is there a way to load CSS or javascript as a string? For example, something like:

var style = ".class { width:100% }"

Or something of that nature.

var javascript = "alert('hello');";

This will load the JavaScript from a string, but be warned that this is highly insecure and not recommended. If a malicious process ever interfered with your string of JavaScript, it could result in security issues.

As far as CSS goes, you can append a style tag to the page using jQuery, like so:

$('head').append("<style>body { background-color: grey; }</style>");

Or, for the JavaScript purists:

var s = document.createElement("style");
s.innerHTML = "body { background-color:white !important; }";

Related Q&A

Jquery Dialog, Adding a new line of styled text

I am trying to create a customer support dialog. I want the dialog to have two lines of text, and a title. The first line will be an error message, and the second will be a bold customer service number…

Extending the function prototype

Im looking to be able to extend the function prototype in javascript (that is to say, add a function to all functions). The purpose is to convert it into a format for exchange between another language …

How do I fire a javascript playsound event onclick without sending the user to the top of the page?

I have the following code on a page on my site — when the user clicks on the image a sound plays:<script language="javascript" type="text/javascript">function playSound(sound…

Can a selection object be created without any user interaction?

Can a Selection object be created without any user interaction? window.getSelection() does return a Selection object, but you cant modify() it unless the user has some sort of selection made. Is it p…

Separate key and value pairs into two arrays

What would be the best way to go about separating the key and values into two different arrays so that this - var data = {"A Key": 34, "Another Key": 16, "Last Key": 10};W…

Identify tab that made request in Firefox Addon SDK

Im using the Firefox Addon SDK to build something that monitors and displays the HTTP traffic in the browser. Similar to HTTPFox or Live HTTP Headers. I am interested in identifying which tab in the br…

Compress data in php and uncompress in javascript [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 9…

Check if javascript file was loaded

I am loading the following javascript files from a CDN. Is there any way I can check if these have been loaded correctly and if not, load them from a local folder?…

Ellipsis to truncate a long text

I am using ellipsis concept to truncate a long text in my HTML. I have successfully managed to truncate the sentence but the "..." wont appear in my HTML. I use the following for the cssThe o…

flattening nested arrays/objects in underscore.js

I have an array of objects as shown below (although the example below has just one element in the array)[{"uptime":0,"load":{"x":0.11,"y":0.22,"z":0.33…