I've got three files and a separate url for each. I want to display the third url (http://example.com/some-other-url), but I need the html from two previous urls displayed as well (they have styles and html that I need in the third url). The files need to remain seperate for the most part, but the JS can be combined
This is already set up:
- The first url makes an AJAX request to the second url on load
- The AJAX request returns all html in the second url
- The second url makes an AJAX request to the third url on button click
- The AJAX request returns all the html in the third url
I was going to solve this by requesting the first url (http://example.com/some-url?option=my-option), then I'd click (button.trigger("click")
) the button in the returned html (this would cause the AJAX request for the third url http://example.com/some-other-url). But I have run into a problem that stops the button from registering the click.
I don't know why, but no matter how many ways I try to trigger the button (or even just the function https://stackoverflow.com/a/3403027/7304372), it never happens. I have no idea why, but it's incredibly frustrating.
Here's the example code for each url:
http://example.com/some-url?option=my-option
<div class="options"></div>
<div class="my-option"></div>
<script type="text/javascript">
const irrelevantData = "Irrelevant file specific data"
const url = "some-other-url"
$.ajax({
url: url,
type: "get",
data: {"irrelevant-data": irrelevantData},
success: function(response) {
// Do other stuff here
$(".options").html(response);
window.history.replaceState("", "", url);
// Perform redirects
let searchParams = new URLSearchParams(window.location.search)
if (searchParams.has("option")) {
if (searchParams.get("option") == "my-option") {
$(".link-my-option").click();
}
}
}
});
</script>
http://example.com/some-other-url
<div class="link-my-option">Click me!</div>
<script type="text/javascript">
$(".options").on("click", ".link-my-option", function() {
const irrelevantData = "Irrelevant file specific data"
const url = "irrelevant-url"
$.ajax({
url: url,
type: "get",
data: {"irrelevant-data": irrelevantData},
success: function(response) {
// Do other stuff here
$(".my-option").html(response);
window.history.replaceState("", "", url);
}
});
});
</script>
http://example.com/some-other-url
<div>Irrelevant html</div>
Please, explain to me why I can't click the button or run the function that's in the returned data of the second url and how I can do both of these things.