简体   繁体   中英

New to JavaScript, function doesn't seem to run (Notify.js)

this is probably very simple but I'm new to JavaScript and not really sure what I'm doing wrong.

Basically I have a profile configuration page on a website and through a form, you can change your nickname, email, preferred method of contact, etc. I was looking for ways to easily display a notification when you successfully change something and found notify.js - unfortunately, couldn't find much help on actually installing/using this besides how to call it's functions and nothing I try seems to work.

I copied the text from this page and put it in a local file called notify.js - it's in the same directory as the rest of the files. https://raw.githubusercontent.com/msroot/Notify.js/master/Notify.js

I'm trying to call the Notify function to display the success/error message but it never seems to show up:

 <html> <head> ...code... <script src="notify.js"></script> <?php if ($nickChanged == 1) { ?> <script> $(document).ready(function() { Notify("An error occurred while saving your new nickname.", null, null, 'danger'); ); </script> <?php } else if ($nickChanged == 2) { ?> <script> $(document).ready(function() { Notify("Successfully changed nickname!", null, null, 'success'); }); </script> <?php } ?> </head> 

Anyone have any idea as to what I'm doing wrong? Again, it's probably simple but this is basically my first time using JavaScript at all

Rendered HTML as per request:

 ...code... <script src="notify.js"></script> <script> $(document).ready(function() { Notify("Successfully changed nickname!", null, null, 'success'); }); </script> </head> ...code... 

Found this error in the browser console: Uncaught ReferenceError: $ is not defined at http:// website .net/sc/profile.php:80:17

In the actual php file, line 80 is CSS but in the rendered page, it's this line: $(document).ready(function() {

Someone mentioned I should add jQuery so I added this line:

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
Still not showing notifications, should that have fixed it?

Try to add bootstrap and font awesome beside jQuery:

<link href="http://getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet">
<link href='http://fonts.googleapis.com/css?family=Cabin' rel='stylesheet' type='text/css'>
<link href="http://fortawesome.github.io/Font-Awesome/assets/font-awesome/css/font-awesome.css" rel="stylesheet">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM