2

On my site I have a number of links to various external booking forms that should open in new windows. Unfortunately I can only change each links class (can't set the Id's).

What I'm trying to do is to come up with a Javascript that has a function for each external site to open in a new window. And some jQuery that executes the proper function for the link clicked with a certain class in it.

This is one of the functions (others have a different name and url):

    function booking-site-1(elt) {
    window.open("https://bookings.domainname.eu/4043/31", "_blank", "toolbar=no,status=no,scrollbars=yes,resizable=yes,top=500,left=500,width=600,height=745");
}

And this is the jQuery

jQuery(".booking-site-1-class").click(function(){
                       alert("Finally!")});

Where the jQuery now successfully shows the alert when I click a link I'd like it to execute the function shown above. I such a n00b that I don't know how to do that.

Hope that someone could give me a few pointers here.

0

1 Answer 1

1

Firstly, function names declared using var/const/let/function can't have hyphens - - in fact, no names declared as such in JavaScript can. Use underscores instead:

function booking_site_1(elt) {
    window.open("https://bookings.domainname.eu/4043/31", "_blank", "toolbar=no,status=no,scrollbars=yes,resizable=yes,top=500,left=500,width=600,height=745");
}

And to make it execute, just add it to your click handling function:

jQuery(".booking-site-1-class").click(function(){
    alert("Finally!");
    booking_site_1($("#randomElement"));
});

As pointed out by Jaromanda X in the comments, you can make functions as a property of the window object, or of any object,

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

11 Comments

Wow, thanks for the super quick reply! I changed the function to underscores and changed the jQuery to this (the alert was just there as a proof of concept): jQuery(".booking-site-1-class").click(function(){ booking_site_1(jQuery("#randomElement")); }); And it works! :-) I'm wondering though what this is for #randomElement
Yes @Rob - the #randomElement was just as a demonstration of how you would use it. You can replace that with any element you wish.
Ah, ok. So I could take that out then?
Since you don't use the elt param in your function, yes, just execute it like booking_site_1().
Thanks! Giving that a try now.
|

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.