0

Trying to link a javascript file to my header.php so users can login via a popup form. The popup form is there, just hidden, and I'm using javascript to show it when the login button is clicked.

The file won't link though?

I've added a basic alert to check if it is at all linked and nothing happens.

<script src="<?php echo BASE_URL . '/assets/js/loginPopup.js'?>"></script>

Does that look wrong to anyone? Proper pulling my hair out, cheers for any help

15
  • 1
    So what does that translate to when you look at the Page Source Commented Jul 17, 2020 at 14:33
  • PHP processes before the page is sent to the browser, javascript afterwards. If something is not loading right, look at the source in the browser to see if it looks correct there first. Commented Jul 17, 2020 at 14:34
  • It looks fine to me, '<script src="localhost/weinspire/blog/assets/js/loginPopup.js"></…>' that is what shows. Commented Jul 17, 2020 at 14:36
  • 2
    Open "Developer tools" in your browser and see if you get any errors in the console. You can also open the "network" tab and reload the page to see what files are loaded and what the status is. Don't forget to empty the cache when you've change your js file as well. Commented Jul 17, 2020 at 14:36
  • 1
    @RiggsFolly - I actually prefer to do it like that, but I tend to start with a /. :-) Commented Jul 17, 2020 at 14:40

1 Answer 1

1

Your URI to the script should start with http:// like

<script src="http://localhost/weinspire/blog/assets/js/loginPopup.js"></script>

or maybe

<script src="http://localhost/assets/js/loginPopup.js"></script>

optionally, relative from the root

<script src="/assets/js/loginPopup.js"></script>

Otherwise, the browser treats this as a relative path to localhost subfolder within the root of your virtual host.

TIP1: As some already pointed in comments, you should also use Inspection tools of your browser like Console and Network, to check why your path is not valid.

TIP2: it's always better to use some local domains like http://myproject.loc instead of http://localhost for many reasons.

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

3 Comments

They mentioned in the comments that they get a js-error in the console in that file, so it seems to be loaded. When they copy/pasted the code into the comment, SO hides http:// when turning it into a link (which it wouldn't have done without the scheme). Here's the difference: as text: localhost, as code: http://localhost
SO hides protocol of link when it's placed in the text, but not, when you placing it in a code block, as can be seen in ma answer. like (both added with protocol) localhost vs http://localhost
Yes, and the OP only posted the generated link in a comment as text, not as code, which is why we don't see the URL scheme. But the URL got turned into a link, which means that it must already have contained the URL scheme. :-)

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.