Jump to content

Javascript doesn't work on website but runs on codepen

Wolly9102
Go to solution Solved by Ominous,

You need to create a dedicated script tag for your JQuery include and your own custom script as such:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" type="text/javascript"></script>
<script>
  // added the above script tag and ended the include script tag on the line above.
  $(document).ready(function () {
        
   $("#slideshow > div:gt(0)").hide();

   setInterval(function() { 
   $('#slideshow > div:first')
       .fadeOut(4000)
       .next()
       .fadeIn(4000)
       .end()
       .appendTo('#slideshow');
   },  3000);
   });
</script>

Behaviour of a script tag with both a src="" attribute and content seems to be inconsistent. Some browsers may run this but its much better to separate them. Your Codepen works because its effectively being compiled into the above.

Hello,

I am breaking my head over this. I've got an simple HTML website running live and I want to put an moving slideshow on it. Now I want to accomplish this with an external javascript and insert it into my HTML. (see code)

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" type="text/javascript">
  $(document).ready(function () {
        
   $("#slideshow > div:gt(0)").hide();

   setInterval(function() { 
   $('#slideshow > div:first')
       .fadeOut(4000)
       .next()
       .fadeIn(4000)
       .end()
       .appendTo('#slideshow');
   },  3000);
   });
   </script>

 

 

 

 

div id="slideshow">
  <img src="---">
  </div>
  <div>
  <div id="slideshow">
    <img src="---">
  </div>
  
  <div>
  <div id="slideshow">
    <img src="---">
  </div>
  
  <div>
  <div id="slideshow">
    <img src="--->
  </div>
  
  <div>
  <div id="slideshow">

    <img src="---">
  </div>

Now when I upload this to the website the slideshow shows but it doesn't do anything. However when I use the exact same code on codepen https://codepen.io/Wolly9102/pen/zYLBodb it works fine.

 

What am I missing here? I am a noob at html so forgive me if it's something stupid.

Main PC:

CPU: AMD Ryzen 5 5600X | RAM:Corsair LPX 3200 mhz (16Gb) 

Mobo:ASUS Strix B550-F Wifi | GPU: MSI RTX 2070 Gaming Z

Case: Sharkoon Nightshark RGB| Storage: 500 GB 970 EVO Plus 1 TB WD blue 500 GB Samsung HDD

Monitor: iiyama G-Master G2470HSU-B1 165Hz

Powersupply: Be Quiet straight Power 10 500 watt

 

 

Main Laptop

CPU: AMD Ryzen 7 4800H | RAM: Team group 16 GB 2666 mhz

GPU: RTX 2060 (MXM swappable)

Monitor: 1080p 120Hz

Storage: 2x 1 TB Samsung 970 EVO NVMe (no raid)

 

 

Second Laptop

CPU: Intel Core I5 1235u,  RAM: Samsung 8 GB 3200 mhz

GPU: IrisXe 80 eu

Storage: 512 GB WD Digital SN530 NVMe

 

Phone:

Xiaomi MI 11

 

Work Phone:

Galaxy A50

 

Link to comment
Share on other sites

Link to post
Share on other sites

Without knowing your webserver im guessing its not Java compatible?

When i ask for more specs, don't expect me to know the answer!
I'm just helping YOU to help YOURSELF!
(The more info you give the easier it is for others to help you out!)

Not willing to capitulate to the ignorance of the masses!

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, HanZie82 said:

Without knowing your webserver im guessing its not Java compatible?

I don't know myself. it's hosted externally and I got the site from someone who quitted with it. I could ask there support it's java supported.

Main PC:

CPU: AMD Ryzen 5 5600X | RAM:Corsair LPX 3200 mhz (16Gb) 

Mobo:ASUS Strix B550-F Wifi | GPU: MSI RTX 2070 Gaming Z

Case: Sharkoon Nightshark RGB| Storage: 500 GB 970 EVO Plus 1 TB WD blue 500 GB Samsung HDD

Monitor: iiyama G-Master G2470HSU-B1 165Hz

Powersupply: Be Quiet straight Power 10 500 watt

 

 

Main Laptop

CPU: AMD Ryzen 7 4800H | RAM: Team group 16 GB 2666 mhz

GPU: RTX 2060 (MXM swappable)

Monitor: 1080p 120Hz

Storage: 2x 1 TB Samsung 970 EVO NVMe (no raid)

 

 

Second Laptop

CPU: Intel Core I5 1235u,  RAM: Samsung 8 GB 3200 mhz

GPU: IrisXe 80 eu

Storage: 512 GB WD Digital SN530 NVMe

 

Phone:

Xiaomi MI 11

 

Work Phone:

Galaxy A50

 

Link to comment
Share on other sites

Link to post
Share on other sites

You need to create a dedicated script tag for your JQuery include and your own custom script as such:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" type="text/javascript"></script>
<script>
  // added the above script tag and ended the include script tag on the line above.
  $(document).ready(function () {
        
   $("#slideshow > div:gt(0)").hide();

   setInterval(function() { 
   $('#slideshow > div:first')
       .fadeOut(4000)
       .next()
       .fadeIn(4000)
       .end()
       .appendTo('#slideshow');
   },  3000);
   });
</script>

Behaviour of a script tag with both a src="" attribute and content seems to be inconsistent. Some browsers may run this but its much better to separate them. Your Codepen works because its effectively being compiled into the above.

Link to comment
Share on other sites

Link to post
Share on other sites

15 hours ago, Ominous said:

You need to create a dedicated script tag for your JQuery include and your own custom script as such:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" type="text/javascript"></script>
<script>
  // added the above script tag and ended the include script tag on the line above.
  $(document).ready(function () {
        
   $("#slideshow > div:gt(0)").hide();

   setInterval(function() { 
   $('#slideshow > div:first')
       .fadeOut(4000)
       .next()
       .fadeIn(4000)
       .end()
       .appendTo('#slideshow');
   },  3000);
   });
</script>

Behaviour of a script tag with both a src="" attribute and content seems to be inconsistent. Some browsers may run this but its much better to separate them. Your Codepen works because its effectively being compiled into the above.

Thanks, That did it ^^

Main PC:

CPU: AMD Ryzen 5 5600X | RAM:Corsair LPX 3200 mhz (16Gb) 

Mobo:ASUS Strix B550-F Wifi | GPU: MSI RTX 2070 Gaming Z

Case: Sharkoon Nightshark RGB| Storage: 500 GB 970 EVO Plus 1 TB WD blue 500 GB Samsung HDD

Monitor: iiyama G-Master G2470HSU-B1 165Hz

Powersupply: Be Quiet straight Power 10 500 watt

 

 

Main Laptop

CPU: AMD Ryzen 7 4800H | RAM: Team group 16 GB 2666 mhz

GPU: RTX 2060 (MXM swappable)

Monitor: 1080p 120Hz

Storage: 2x 1 TB Samsung 970 EVO NVMe (no raid)

 

 

Second Laptop

CPU: Intel Core I5 1235u,  RAM: Samsung 8 GB 3200 mhz

GPU: IrisXe 80 eu

Storage: 512 GB WD Digital SN530 NVMe

 

Phone:

Xiaomi MI 11

 

Work Phone:

Galaxy A50

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×