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 8084103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:41:17+00:00 2026-06-05T17:41:17+00:00

Having trouble accessing javascript code in a mixed html/js ajax response. jQuery ajax doc

  • 0

Having trouble accessing javascript code in a mixed html/js ajax response. jQuery ajax doc states:

If html is specified, any embedded JavaScript inside the retrieved
data is executed before the HTML is returned as a string

Which I can confirm by adding a simple snippet to the html reply:

<script type="text/javascript"> alert($(this)); </script>

How then to retain access to the js code vs. one-and-done execution?? Trying to implement a modal login (to prevent data loss on session timeout in form submission screens). Of course I need to be able to access the ajax’d js code to then validate email/password fields and ajax authenticate user credentials on the remote server.

Here’s the modal login coffeescript snippet:

# submit form
$.ajax
  success: (data) -> ...
  error: (data) ->
    popAuth(data.responseText) if(data.status == 401)

popAuth = (title) -> 
  $.fancybox({
    href: "/login"
    ajax: { type: "GET" }
    title: title
  })

Perhaps I can add a success callback to popAuth() ajax options to store the returned js code? How about jQuery “live” handler? Unfortunate that this scenario is not as straight forward as one would hope 😉 I have seen $.getScript as an option, but would prefer to not separate html from js since server-side already assembles html + js and the original ajax call pulls it all down in one go. (i.e. avoid creating a dedicated server-side controller to send back js file content bundle)

I am of course open to alternative solutions to workaround this issue. For example, I could store login fields and js login validation code on every screen (JVM CRUD application living behind WordPress front end so every screen is basically auth required) in a hidden div, and then pop the modal login window “locally”, which I assume would get around the annoying one-and-done js execution of remote ajax content.

Anyway, Ideas appreciated! client-side is both wonderfully simple and…horribly complex 😉

  • 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-05T17:41:18+00:00Added an answer on June 5, 2026 at 5:41 pm

    Ok, fending off the veritable deluge of responses, I’ll take a stab myself.

    As I understand it now, since mixed html/js content is one-and-done executed, we have one chance to capture ajax response js code and bind it to current scope.

    First, in the original ajax call (i.e. form submit that returns a potential 401 not authorized status) set the context of the modal login’s ajax setup to $(this), the currently executing scope that contains jquery validation and other shared js code needed for modal login ajax submit to work.

    In my case, using fancybox, adding context param it now looks like:

    popAuth = (title) -> 
      $.fancybox({
        href: "/login"
        ajax: { type: "GET" }
        context: $(@)
        title: title
      })
    

    Then, since the parent window contains the majority of needed javascript, the only requirement is to create a js file that binds modal login form button click event to validation and $.ajax submission.

    # login.coffee
    jQuery ->
      $('#loginSubmit').click (e) ->
        e.preventDefault()
        isValid = $('#loginForm').validate().form()
        if isValid
          $('#spinner').show()
          $.ajax
            data: $('#loginForm').serialize()
            success: (data) ->
              $('#status').fadeOut()
              location.href = '/foo'
            error: (data) ->
              $('#status > div').html( data.responseText )
              $('#status').fadeIn()
            complete: () ->
              $('#spinner').hide()
    

    Done, all good, works 😉

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

Sidebar

Related Questions

I am having trouble accessing a Javascript function from my code behind file. I
I am having trouble accessing the value of an HTML Input element using jQuery,
I'm converting some javascript to coffeescript, and I'm having trouble accessing a function I've
I'm having trouble accessing the review_history using the getInfoFor method, example code below :
I'm having some trouble accessing the data attributes of a dynamically(ajax) loaded file. For
I'm having trouble accessing javascripts within a jQuery dialog. For example, page A calls
I'm having trouble accessing an array created in php, to use in a javascript
I am having trouble accessing the method fromString in Scala. I tried javap and
I am having trouble accessing the data field. I receive the error: Databinding methods
I'm having trouble accessing the id , area and theme values in my ViewData.

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.