Archived topic
Adding JS to Page
5 replies · Started by Mike on April 3, 2018
Hello,
My website https://gladstonevillagehall.org includes four lightweight grid columns with backgrounds and hyperlinked text within them. I am trying to allow the user to be able to click anywhere within the background and be taken to the link that the text contains. So far I have found the following JS able to do this,
<script>
$("div.lgc-column:nth-child(1)").click(function() {
window.location = $(this).find("a").attr("href");
return false;
});
</script>
<script>
$("div.lgc-column:nth-child(2)").click(function() {
window.location = $(this).find("a").attr("href");
return false;
});
</script>
<script>
$("div.lgc-column:nth-child(3)").click(function() {
window.location = $(this).find("a").attr("href");
return false;
});
</script>
<script>
$("div.lgc-column:nth-child(4)").click(function() {
window.location = $(this).find("a").attr("href");
return false;
});
</script>
I am however struggling to impliment it. I have previously had it working, however after removing my smooth scrolling JS it is now refusing to work!
Do you have any suggestions on how I could either better go about achiving what I wish or do you know of a way to insert the JS code? I have tried various snippit plugins, nothing seems to be working.
Thanks,
Mike
I would personally give each column a class (the same class), then do this:
<script>
jQuery( document ).ready( function($) {
$(".your-custom-class").click(function() {
window.location = $(this).find("a").attr("href");
return false;
});
} );
</script>
Hello Tom,
Thanks for that code, it works a treat! I added the same class to all of the columns and used your code, far more efficiant than four diffrent scripts!
On another note, have you any ideas on how I can tune my https://gladstonevillagehall.org website to get better performance? I was able to get the loading times really down on my company site https://hosker.tech by compressing images etc, however after trying all those steps for https://gladstonevillagehall.org it seems still very slow to load. Any ideas?
Thanks,
Mike
Pingdom is great for that. Check the file requests section here: https://tools.pingdom.com/#!/c38k3I/https://gladstonevillagehall.org/
It will tell you which files are taking the most time to load.
Hello,
I suspect it may be the add this social widgets! Have to say, I am surprised at how much overhead they have, especially considering they are supposed to be developed professionaly.
Think I will try removing the add this plugin and see what performance diffrence it gives.
Thanks,
Mike
Those kinds of plugins can be brutal when it comes to performance.
Glad I could help :)