1

The website I'm working on has hundreds of existing pages, and I want to insert an Autocomplete feature into every page. I don't want to have to put the Javascript <script src=> call into the hundred of pages. The field using the JS is contained in the Nav which is called on every page from a php include, so getting the HTML in is no problem.

There are a few places I think I could put the Javascript in - 1. Either add the Javascript functions to an existing Javascript Script that is called in the header,

2. or even put in the <script> call in an existing php include that calls the $_SESSION and mysql data to everypage, but that is called even before the <!DOCTYPE> declaration, so I don't think I would want to put it there.

3. But, because I would like to keep it in it's own file (In case it needs replacing, tidiness, etc) I want to keep the autocomplete Javascript in it's own file. Would it be ok to put a <script> call inside of an existing <script call>? I hope that is clear.

4. OR, put the <script src> inside the nav.php (which is called on everypage). But I'm not sure how well putting a PHP include inside of a Javascript file would work out.

If anyone has any opinions or advice as to which would work the best, please let me know. Thanks!

2
  • I think #3 is my best option 1- takes the JS out of its own file, 2- is probably bad because it is called before the <!DOCTYPE>, and 4. involves mixing PHP/JS calls. Any feedback? Commented Oct 11, 2013 at 18:03
  • Or is it bad practice to put a Javascript call inside of a Javascript call? Commented Oct 11, 2013 at 18:07

1 Answer 1

1

Best option is to create jquery plugin and use it where ever required. Dont forget to make proper use of $ or jquery, because it may crash between plugin and script file in which plugin is placed.

Sign up to request clarification or add additional context in comments.

3 Comments

It's required on hundreds of existing pages. How to put it in all of them simply?
I think you're right. Do you favor any of my aforementioned methods of either 1,2,3,4 for inserting that call into hundreds of pages?
You can do one thing. If your are using some kind of master page then at the page load of master page you can register that script, like ClientScript.RegisterStartupScript is used in asp apps to call js file from code behind page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.