0

I am trying to load a popup lightbox window when the page is initially opened. I can get it to work on a click but I cannot get the code right to add a class in the script. I am using 'Lightbox' from within HTML Kickstart.

The link that works looks like this: <a class="lightbox" href="#bbc">BBC</a> This works perfectly. Basically how do I get that to work automatically on page load.

My attempt at the script:

<script type="text/javascript">
    function load() {
        var target = location.href = "#bbc";
        target.className = "lightbox";
    }
</script> 
3

4 Answers 4

1

So I assume you want to add a class to that anchor tag:

$(function () {
    $("a[href=#bbc]").addClass("lightbox");
});

Using $(function() {…}) is the same as using the ready() function (in JQuery). I would recommend running the code after the DOM is ready rather the "on load".

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

Comments

0

you need to call the load() function on the onLoad event of <body> tag.

<body onLoad="load()">

Comments

0

The way I read this question — ignoring the title — is that the user is trying to trigger the lightbox on load. Whilst this may be a wrong assumption, the way to trigger a link using javascript is to use the .click() method:

window.onload = function(){
  var i, list = document.getElementsByTagName('a');
  for ( i=0; i<list.length; i++ ) {
    /// this will only work for links with only a class name of lightbox.
    /// you could look into using getElementsByClassName or something similar.
    /// I use getElementsByTagName because I know how supported it is.
    if ( list[i].className == 'lightbox' ) {
      list[i].click();
    }
  }
};

The above code would support multiple lightbox links in a page, which might not be desired, so it may be best just to add an id to the link you wish to target and then use:

window.onload = function(){
  document.getElementById('clickonload').click();
};

<a id="clickonload" class="lightbox" href="#bbc">BBC</a>

You may find however, that in reading the documentation for whatever lightbox plugin you are using, that there is a command you can use from JavaScript, rather than clicking a target link.

Comments

0
$(window).load(function () {
    var target = location.href = "#bbc";
    target.className = "lightbox";
});

5 Comments

I think OP wants using JavaScript not jQuery.
@user2428069 : But I think using jQuery will much easier than core JavaScript
@PradipKharbuja HTML Kickstart is a jQuery library so it's fair to assume the OP can use a jQuery solution. And please don't use code formatting for something that isn't code.
Sorry if he must not use jQuery, i always forgot to check if it needed. But my solution should work.
Why do you define the function load inside the event handler and call it immediately? Just put the two lines of code directly in the event handler. No need to define a second function.

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.