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 8772487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:02:42+00:00 2026-06-13T18:02:42+00:00

I would like to move JS code snippets out of PHP files and into

  • 0

I would like to move JS code snippets out of PHP files and into their own files in order to make code a lot cleaner and easier to maintain. For example, I have this function:

function load_script($fieldname)
{
  return
  'var help = function () {alert(' . '"EDITOR HELP:\n\n' . 
                                     'blah blah.\n\n' . 
                                     'more blah blah.\n\n' . 
                                     ');}' . "\n" .
  'var options = { handler: help, title: "Editor help" };' . "\n" .
  'var converter = Markdown.getSanitizingConverter();' . "\n" .
  'var editor = new Markdown.Editor(converter, "-'.$fieldname.'", options);' . "\n" .
  'editor.run();' . "\n";
}

Notice the $fieldname PHP variable.

The idea is to store the JS portion in a .js file. Then I’d read it in as plain text in order to output in the return statement. In other words, something like this:

function load_script($fieldname)
{
  $output = file_get_contents("load_script.js");
  return $output;
}

Obviously the problem is that this would not substitute $fieldname with the corresponding value.

My current thought on this is to run $output through string subsitution:

function load_script($fieldname)
{
  $output = file_get_contents("load_script.js");
  $output = str_replace("some_unique_identifier", $fieldname, $output);
  return $output;
}

Is there a better approach?

EDIT:

I should add some of the motivation behind this:

First, the example given is ugly and hard to maintain for anything but the simplest JS snippets. Lots of room for mistakes.

Second, editors aren’t very helpful in terms of checking syntax and highlighting when you mix things up this way.

Third, having JS live on its own files makes it easier to run a minification script that crunches on the entire site (so you don’t have to manually maintain minification).

  • 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-13T18:02:43+00:00Added an answer on June 13, 2026 at 6:02 pm

    You could use a template engine to do this (such as Smarty for instance). Simply write your JS files using the template variables where your PHP variables would be, and load them through PHP.

    You could even roll your own simple one. Something like:

    // in JS file:
    function myFunc() {
        alert("hello %s");
    }
    
    // in PHP file:
    <?PHP
        //... code to open JS file and load into string....
        echo sprintf($contentsOfJSFile, "John Doe");
    ?>
    
    RESULTING OUTPUT: 
    
    function myFunc() {
        alert("hello John Doe");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to move some files around in Visual Studio. But I see
I'm re-factoring my code and would like to move a whole bunch of UILabels
Here is my code right now. But I would like to move those Add
I would like to accomplish two tasks at once. First, to move my snippets
I would like to move the following piece of code from a c# aspx.cs
The following code works, but i would like to move ostream& operator<< outside of
I would like to move files from the Windows Explorer to my Java GUI
We would like to move our code from the local SVN server to a
I would like to move a circle along a circular path using HTML/CSS/JavaScript. Is
I would like to move data back and fourth between clojure applications. Application settings

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.