Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7978671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:33:07+00:00 2026-06-04T09:33:07+00:00

I have a remote javascript file containing a custom jQuery event handler. When this

  • 0

I have a remote javascript file containing a custom jQuery event handler. When this is inserted into the DOM as a <script src='...'></script> element, the custom event handler is not registered.

However, if I add the exact same script as straight JS (<script>...</script>), the event handler is registered, and everything executes as expected.

Why isn’t the event handler being bound in jQuery when the script is inserted as a remote file?

Example

The remote file, containing the custom event handler:

console.log('Before custom event handler');
$('button').on('customEvent', function(){
    console.log('customEvent Caught');
});

https://gist.github.com/2767385

The (non-working) javascript which inserts the script into the DOM:

var scriptNode = document.createElement('script');
    scriptNode.src = 'https://gist.github.com/raw/2767385/b9ddea612ff60b334bd2430e59903174e527a3b5/gistfile1.js';
document.body.appendChild(scriptNode);

The (working alternative) javascript which inserts the script as inline into the DOM:

var scriptText = "console.log('Before custom event handler'); $('button').on('customEvent', function(){ console.log('customEvent Caught'); });",
    scriptNode = document.createElement('script');
scriptNode.appendChild(document.createTextNode(scriptText));
document.body.appendChild(scriptNode);

Triggering the event:

$('button').triggerHandler('customEvent');

The JS is correctly read, and the handler is correctly executed.

JSFiddles

Remote file – non-working example: http://jsfiddle.net/3CfFM/3/
Using Text – working alternative: http://jsfiddle.net/3CfFM/2/

What’s happening?

Why isn’t the event handler being bound in jQuery when the script is inserted as a remote file?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T09:33:08+00:00Added an answer on June 4, 2026 at 9:33 am

    You’re mistaken. The event handler is being bound when used with a remote script; it simply takes a little longer. The browser needs to make an HTTP request before it binds the handler. This means that the original event you trigger with triggerHandler('customEvent') is not captured, since its bubbling and capturing has already completed.

    If you wait a second, then click the button again, you will see that the event handler has indeed been bound. You can also see this by delaying your triggerHandler call until the script has loaded:

    $('button').click(function() {
        var scriptNode = document.createElement('script');
        scriptNode.src = 'https://gist.github.com/raw/2767385/897cffca74411dbb542c0713bacb5a4048d6708b/gistfile1.js';
        scriptNode.onload = function() {
            $('button').triggerHandler('customEvent');
        };
        document.body.appendChild(scriptNode);
    });
    

    http://jsfiddle.net/3CfFM/4/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small project containing 1 HTML file and 4 JavaScript files. Recently,
For simplicity, i have the following file named test.jsp: <script language=javascript> alert(a$b.replace(/\$/g,k)); </script> I
Did not have luck with these examples: Javascript File remove Javascript FSO DeleteFile Method
My problem: I have an JSON Array containing arrays, which i render into the
I have a script element in my webpage, something like this: <script id=myscript></script> Now,
just for fun... Is it possible to have a .js file that loads jquery
Been struggling with this one I have the following DOM Structure: <ol id=add_images> <li>
I have an index.html.erb file with the following: <%= render @users %> This renders
Is it possible to load remote JavaScript files from the address bar? I have
I'd like to ask if this 'synchronizing-scenario' is possible with php and javascript/jQuery... I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.