Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8476413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:10:19+00:00 2026-06-10T18:10:19+00:00

I am trying to create a Javascript file that I can reference from other

  • 0

I am trying to create a Javascript file that I can reference from other domains that will allow the look and feel of a user’s page to be changed.

For example, The javascript file (www.mywebsite.com/123.js) would be referenced on http://www.userwebsite.com and could contain the below:

var thebody = document.getElementsByTagName('body')[0];
var newdiv = document.createElement('div');
var txtNode = document.createTextNode("Hello. This is a new node.");   
var divIdName = "mydiv";
var csslink = "http://www.anotherwebsite.com/style.css";
var newcss = document.createElement('link');
newcss.setAttribute('href',csslink);
newcss.setAttribute('rel','stylesheet');
newcss.setAttribute('type','text/css');
newdiv.setAttribute('id',divIdName);
newdiv.appendChild(txtNode);
thebody.appendChild(newdiv);
thebody.appendChild(newcss);

Which would insert a reference to a CSS file hosted on the user’s website as well as add a new div to the DOM with the text node “Hello. This is a new node.”

I want the user to be able to login to a CMS (that I will build) so they can change the text that gets printed to the page without having to change the Javascript file that I give to them. The CMS would be hosted on my website.

What is the best practice to do this?

Obviously I could edit the javascript file remotely without the user needing to change anything – but is that the best method of updating the script?

I was thinking of having an Ajax request in the javascript that retrieves output from a php file referencing a mysql database with the text the user wants to change to – however, the same origin policy means that isn’t possible.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T18:10:21+00:00Added an answer on June 10, 2026 at 6:10 pm

    You don’t need to use AJAX to retrieve the PHP output; get the clients to add a <script /> tag to their pages with the src attribute set to your PHP output; just make sure your script emits valid JavaScript.

    As your web server now thinks it’s in PHP land rather than JS assets, be sure to emit correct page headers; MIME type and expires spring to mind.

    header('Content-Type: application/x-javascript');
    header('Last-Modified: Mon, 03 Sep 2012 22:33:44 GMT');
    

    Furthermore, be aware of the un-reliable, and “hostile” environment your script will be executing in. You should minimize the amount of global variables your JavaScript script declares to mitigate the risks of collisions. The easiest way to do this is to wrap your code in an IIFE;

    // Look ma, no global variables!
    (function () {
    
    var thebody = document.getElementsByTagName('body')[0];
    var newdiv = document.createElement('div');
    var txtNode = document.createTextNode("Hello. This is a new node.");   
    var divIdName = "mydiv";
    var csslink = "http://www.anotherwebsite.com/style.css";
    var newcss = document.createElement('link');
    newcss.setAttribute('href',csslink);
    newcss.setAttribute('rel','stylesheet');
    newcss.setAttribute('type','text/css');
    newdiv.setAttribute('id',divIdName);
    newdiv.appendChild(txtNode);
    thebody.appendChild(newdiv);
    thebody.appendChild(newcss);
    
    }());
    

    You should also consider prefixing your CSS classes, element ID’s etc to minimize collisions with the hosts naming schemes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a custom javascript class that inherits from google.maps.Map (V3 API).
I am trying to create a JavaScript regular expression that can find a keyword
I'm having a problem trying to create a Javascript function that checks all the
I am trying to create a simple Javascript text editor that only makes paragraph
Hello there i'm trying to create a form with Zend_Form that will enable my
I've been trying to create my own firefox toolbar with commands that will open
I am trying to retrieve data from an XML file that is not located
I am trying to create an array in my JavaScript object that contains objects
I'm trying to create a gem that wraps d3.js, Source can be found at
I am trying to create a link that people can put on their own

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.