-2

How can I trigger an javascript (or jquery) event when I go to other page?

For exemple:

  • I'm at index.html
  • At index.html, I clicked on contact.html link;
  • When I go to contact.html page, I need to trigger an event

Thanks for for your attention

EDIT:

I will try to explain in other way:

I am working with RubyOnRails. I have a page where I can add contacts. There are a lot of fields but for this example I will use only two:

<select name='type' id='type_select'>
    <option value='0'></option>
    <option value='1'>Home</option>
    <option value='2'>Work</option>
</select>
<input type="text" name="contact_name" id="contact_name_input">

On my Javascript I have:

// this only works if I press CTRL+F5 to refreash the page
$(document).ready(function(){
    $("#contact_name_input").hide();
});
// This Works fine
$('#type_select').change(function(){
    if ($(this).val() == 1 || $(this).val() == 2){
        $("#contact_name_input").show();
    } else {
        $("#contact_name_input").hide();
    }
});

In other words, what I need is to hide the input field when the user visit the page and to show it only if he selected a valid type.

I hope to have been more clearer

Thanks for your attention

WeezHard
  • 1,982
  • 1
  • 16
  • 37
  • According to my understanding's http://stackoverflow.com/a/7080331/2260614 might help..!! – Bhavik Jun 11 '14 at 11:23
  • kapa and @ElmoVanKielmo.. This question is not duplicate of the one that your sugest. My Problem is diferent. when I visit for the first time a page, the document.load event is called. But at the same site, if I click another page, this document.load is not called again. I have to refreash the page (CRTL+f5) to call again the document.load event – WeezHard Jun 11 '14 at 12:10
  • @PSantos If there are additional details / example code that you can add to your question to improve it, please do so. – xDaevax Jun 11 '14 at 12:20
  • Hi @xDaevax , let me try to explain with more details what I want: - I have a page: contact.html.erb and on that page I have a div: `
    ....
    ` - on my "app/assets/javascript/application.js I have this code: ` $(document).ready(function(){ $("#message_box").hide(); } ` If I visit localhost:3000/contacts/contact for the first time, the javascript works. If I press CTRL+F5 it works too, but if I come from other page, like localhost:3000/contacts/index it doesn't work
    – WeezHard Jun 11 '14 at 12:24
  • @PSantos It is difficult to read code in comments. Please edit your question to add the additional detail. Also try to include the most complete and working code samples you can to aid others in answering. Please also see this blog post for help on this subject: http://msmvps.com/blogs/jon_skeet/archive/2010/08/29/writing-the-perfect-question.aspx – xDaevax Jun 11 '14 at 12:27
  • Hi @xDaevax I just edited my question – WeezHard Jun 11 '14 at 12:52
  • @PSantos that's what we want on SO. I revoked my downvote and I also voted for reopening your question. – ElmoVanKielmo Jun 11 '14 at 13:06
  • 1
    What about hiding it with CSS and showing it only in the change event? – MrUpsidown Jun 11 '14 at 13:59

1 Answers1

1

It seems like you want to have an event triggered on the target page (after the user has clicked on a link) and is taken to the destination page. If that is the case and you want to trigger a specific event, as others have suggested, your first entry point would probably be document.ready of the new page and you can use a querystring to specify a more specific event.

On Index.html

<a href="contact.html?e=1">Contact</a>

On Contact.html

<script type="text/javascript">
    $(function() {

        $(document).bind("myCustomEvent", function(e, data) {
            alert("Weee, you triggered event id: " + data.EventId + "!");
        });

        var eventId = getParameterByName('e');  // Load the query string specified by the previous page's link
        if(eventId == 1) {
            $(document).trigger("myCustomEvent", {EventId: eventId});
        } else {
        } // end if/else
    });

    function getParameterByName(name) {
        name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
        var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
        return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
    }
</script>

Credit -- Javascript QueryString function shamelessly taken from here: How can I get query string values in JavaScript?

Posting the solution Given by @xDaevax on chat:

By using the JQuery on $(document).ready(); to hide the input, you will sometimes see a flicker. You may want to consider adding some CSS to the input to hide it then, it will always be hidden when you come to the page. When yo uuse the JQuery show() and hide(), they will change the "display" css attribute from "display: block", to "display: none" depending on whether you call show() or hide() so, in your javascript, you could try:

$("#type_select").change(function(e) {
    var selectedValue = $(this).val();
    if(selectedValue <= 0) {
        $("#contact_name_input").hide();
    } else {
        $("#contact_name_input").show();
    }
});

It works for me

Community
  • 1
  • 1
xDaevax
  • 2,012
  • 2
  • 25
  • 36
  • Perhaps the answer down voter would be able to elaborate on the reason? Is there something in my answer that can be improved? – xDaevax Jun 11 '14 at 11:44
  • ElmoVanKielmo I am so sorry if the question is unclear. I am angolan and I dont speak english very well. – WeezHard Jun 11 '14 at 12:00
  • 1
    So to be clear, it is not the accuracy of the answer that was given based on the current understanding of the question, it was the principle of the answer being given at all on a question needing improvement that caused a downvote? Perhaps I am missing the part of the question that is unclear. – xDaevax Jun 11 '14 at 12:05
  • @xDaevax Yes. We should not appreciate questions like this. No research effort. No code sample. _Please write code for me_ is not what we want on SO. Answer for such question implies it's ok. And actually this question is a duplicate. I've revoked my downvote for your answer because I hope you understand the reason now. – ElmoVanKielmo Jun 11 '14 at 12:37
  • Yes, I understand your position now. I'm not sure I understand the duplicate qualification, as I stated in my comments on the question, but I understand the spirit of what you're saying. – xDaevax Jun 11 '14 at 12:44
  • @ElmoVanKielmo I think that I dont need code when all I need is the concepts. I am not asking for someone do it for, only to explain the concepts behind it. – WeezHard Jun 11 '14 at 12:54
  • @PSantos I'm sorry but SO has it's rules. Please don't take it personal. Instead please take a look at http://stackoverflow.com/help/on-topic and http://meta.stackoverflow.com/questions/260083/people-who-answer-questions-that-are-clearly-off-topic – ElmoVanKielmo Jun 11 '14 at 13:03
  • 1
    @PSantos Let's chat and not fill up comments on this. https://chat.stackoverflow.com/rooms/55433/room-for-xdaevax-and-psantos – xDaevax Jun 11 '14 at 13:07