|
79ff761fdce50c0e0329fa54f3dab84a
|
Random image Javascriptby Beardedbaglady - Member - 01:04PM, Aug 20, 2007 |
|
Hi there, I’m quite new to Javascript and even newer to Shopify so here goes!.... I simply need to call up a random image on refresh on my page.theme using javascript. These will be illustrative images (hard coded, I guess), rather than (dynamic) product images. I’ve done this on a standard HTML page before but I know I’m doing something wrong when it comes to liquid templates. At the moment I’ve linked to {{ ‘randomimage.js’ | asset_url | script_tag }} in the <head> tag of theme.liquid, which contains the following:
And on page.liquid I’ve added:
to call up the js function in the right place. But I know somewhere I’m going really wrong, either on the js file itself (where i reference the images) or the the page template (do I need to enclose the script in special characters?)... I know the solution is probably very obvious I’m really stuck here! I’d be very grateful for any help. Thanks, Sian |
|
|
I think you need to call it with window onload.
You can check out this thread from a while back to make sure you javascript is correct. http://forums.shopify.com/categories/2/posts/10722 ---
Dan — Last edited 05:25PM, Aug 20, 2007 |
|
|
I don’t think you can embed liquid into a .js file like that. If you want to use that, it would be better to put the script into one of the liquid templates.
|
|
|
That’s correct Matt. I use this code to randomly show a customer feedback quote and all the code is inline in theme.liquid. ---
Dan
|
|
|
In other words, Siad is doing it incorrectly? I’m confused. I am trying to do the same as Siad without luck. If I cannot embed liquid into a .js file, then my .js file should look like this? And then I added DanW’s “showimage” code to the header div in the theme.liquid file. I’ve checked the previous post and the code looks the same. Unfortunately, it doesn’t work. If anyone has any suggestions, please let me know? Thanks, — Last edited 12:38AM, Aug 22, 2007 |
|
|
What you want to do is drop the .js file, and put your functionality (with the liquid tags) into the header of your theme.liquid file as an inline script.
|
|
|
Try it like this. All inline where you want the image to show ---
Dan — Last edited 01:57AM, Aug 23, 2007 |
|
|
Hi Dan, I copied and pasted all your code and put it into the proper places but it still doesn’t work. I renamed the images to match your names and I added the default-image to the randimage div. Here are my calls to the javascript files:
and here is the code that I put where the image is suppose to show:
Can you see anything incorrect here? I’ve been trying all kinds of things included trying to make it all inline but nothings working, and I really appreciate everyones help. — Last edited 11:04PM, Aug 22, 2007 |
|
|
Ok. I took a deeper look and the code I posted was way off. I used my test store and came up with the correct solution. I couldn’t get it to work by calling it from a js file so I just put it inline. Works like a charm. If you renamed your images to mimick my previous code, then you should be able to just copy and paste this in place of your randimage div tag and it should work. I updated my post above with the updated code so no one would try use the old code. ---
Dan — Last edited 02:08AM, Aug 23, 2007 |
|
|
Well it works for me – I think we can consider this case solved, MacGyver! And no duct tape in sight… Thank you danW and Matt. Sian
|
|
|
Copy, paste, refresh, and it works beautifully. Thanks guys for your help.
|
|
|
I could get your code to work no problem, but what I need is to randomly change out a background image. I can’t seem to get this working. Help! This is what I have:
|
|
|
try this
---
Dan — Last edited 08:31PM, Sep 20, 2007 |
|
|
Nope, still no joy.
but that didn’t work either. Any more ideas?
|
|
|
Also.. maybe I should mention, I plan to change out the background image for the <body> of the page. I was hoping once I got the div working, it would also work giving the <body> tag the same id.
|
|
|
Sorry nadia I don’t. My javascript kung fu isn’t all that good. :( ---
Dan
|
|
|
Hey Nadia I may be way off here, depending on your desired outcome, but you might be able to butcher this script from A List Apart which allows you to switch between alternate stylesheets. It should be relatively straightforward to assign 3 css sheets (each of which only specify the body bg image) as possible ‘alternate’ stylesheets and load them in after your main css in the head. Then write a little js function called when the page loads which picks one of the bg stylesheets at random. If you want the bg image to randomise on every page, you wouldn’t even have to worry about cookies. Is that worth looking into? — Last edited 02:52PM, Sep 27, 2007 |
|
|
Thankyou!! It was worth looking into .. and it worked! I took out all the cookie stuff in the stylesheetswitcher.js, and replaced the window.onload with my get_random function. Worked perfectly. I think I may be linking to the alternate stylesheets in a messy way – I have hard-coded the links because I didn’t know how to call an alternate stylesheet tag. But oh well, as long as it works!
|
|
|
Great! glad it worked. I’ve used the technique to switch stylesheets on browser resize, so I was pretty hopeful. You should be able to use asset_url, which would be a little more future-proof if JP switch their servers around in the future:
— Last edited 12:31PM, Sep 28, 2007 |

danW
Shopify Advisor
03:38PM, Aug 20, 2007