Tips for loading JavaScript and CSS files in a custom popup loader

Within the scope of my project, I have implemented a custom pop up page using JavaScript. This pop up includes a form for users to fill out. At the same time, I also need to load certain JavaScript files when the pop up is initialized. Can you please advise on how to achieve this?

<html>
     <head>
           <script type='text/javascript' src='files/jsfiles/core.js'></script>
     </head>
     <body>
           <!- To initiate javascript function showUsers() from here ->
          <input type='button' onclick='showUsers();' value='listUsers'>
     </body>
</html>

Answer №1

function createScriptElement(url) {
    var script = document.createElement("script");
    script.async = false;
    script.src = url;
    document.head.appendChild(script);
};

function createStyleElement(url) {
    var style = document.createElement("link");
    style.rel = "stylesheet";
    style.href = url;
    document.head.appendChild(style);
};

function loadExternalFiles() {
    var scriptsArray = ['src1url', 'src2url'.., 'scrnurl'];
    var csssArray = ['src1url', 'src2url'.., 'scrnurl'];
    for (var i = 0; i < scriptsArray.length; i++) {
        createScriptElement(scriptsArray[i]);
    }
    for (var i = 0; i < csssArray.length; i++) {
        createStyleElement(csssArray[i]);
    }
};

function submitFunction(callback) {
    // your functionality here;
    callback();
}
$(document).ready(function () {
    // do your stuff
    submitFunction(loadExternalFiles);
});

Also check: Another short form

Answer №2

If you're looking for a sleek and easy-to-use library, check out fancybox.

One way to load jQuery is from a CDN:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript" src="/fancybox/jquery.fancybox-1.3.4.pack.js"></script>
<link rel="stylesheet" href="/fancybox/jquery.fancybox-1.3.4.css" type="text/css" media="screen" />

Add this HTML code to create a pop-up page:

<a href="pop up page" onclick='openmsg_sent();' class="iframe open_pop">Sign in</a>

Use the following JavaScript code to activate fancybox functionality:


$('.open_pop').fancybox({
});

function openmsg_sent() {
    jQuery(".open_pop").trigger("click");
}

Answer №3

To ensure a more cohesive UI experience, consider using a div element that can be displayed with fixed positioning when the button is clicked.
I suspect that an Alert might not have the capability to display CSS properties if I am correct.

One approach you can take is to place your popup content inside a hidden div in the JS code and then reveal it upon clicking the button.

I hope this suggestion proves helpful.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Changing the text color using jQuery is proving to be difficult for me

I've been trying to apply a styling condition using an if statement in my jQuery code based on the result from an API, but for some reason, I'm not seeing the color change. I have given an ID to try and change the color through CSS. $.getJSON(API ...

Defeat a JavaScript function or turn it into a Singleton Function

Is there a method to stop a running JavaScript function? Or is there a way to make sure that only one instance of the function runs at a time and any previous instances are removed upon restart? For example, if I call: _.defer(heavyDutyPaint); //How ca ...

Is it possible to translate the IIFE functions of threejs into ES6 format?

I'm currently facing a challenge in breaking down my threejs project into smaller modules. One specific function that I'm struggling with is the following: var updateCamera = (function() { var euler = new THREE.Euler( 0, 0, 0, 'YXZ&apos ...

completing data tables and presenting them to clients

I am currently working on a website for a Nutrition specialist who requires a monthly diet regimen table with five meals daily. The doctor will be responsible for completing these tables, which will then be presented to clients. It's important that th ...

Tips for creating an endless loop within a nested v-for in Vue?

Here is my attempt at solving the problem: HTML <ul class="tree"> <li> <span>First Node</span> <ul> <li v-for="(tree, i) in trees" :key="i"> <span v-text="tree. ...

Using expect() within the .then() function when writing Jasmine unit tests for AngularJS

I'm currently struggling with the .then() function while trying to implement Jasmine unit testing. Here is the code that's giving me trouble: describe("getBuilding", function () { it("checks getBuilding", function () { var id_building = 4; ...

Can the font family of <body> be customized in Bootstrap 5?

I am facing an issue where the Font-Family changes for my heading tags, but the body tag keeps getting cancelled out by the error "_reboot.scss:50". When I inspect my webpage and toggle off the default "enter code herefont-family: var(--bs-body-font-family ...

Why is the $match function in Mongoose not functioning properly with an array of ObjectIds in Node.js

Greetings! I've been trying to match an array of objectIds in Mongoose using Node.js, and although I achieved the desired result in the Mongo shell, I'm facing difficulties when implementing the same in my code. Here's a snippet from my col ...

Removing the gridlines in a Linechart using Apexcharts

I am experiencing issues with the grid and Nodata options on my Apexchart line chart. noData: { text: none , align: 'center', verticalAlign: 'middle', offsetX: 0, offsetY: 0, style: { color: undefined, fontSize: &apo ...

What is the method of generating an HTML tag solely using CSS?

Looking to style HTML tags without altering them in any way? Consider this example: <div id="Code_G" class="editor-group"> editor-group<br /> <div id="Code_L" class="editor-label "> editor-label </div> < ...

Hover effect not displaying upon mouse exit

I am working on a feature where a series of images change when hovered over, with a div animating as an overlay on the image. Here is the code snippet: // hiding overlays initially $(".mini-shop .item .image a div").hide(); // toggling overlay and second ...

Tips for achieving a design aesthetic similar to that of the JQuery UI website

On my website, I am using jQuery UI along with the "Smooth Default" CSS theme. However, I have noticed that everything appears larger compared to the jQuery UI website itself. For instance, when looking at the buttons here: http://jqueryui.com/button/ The ...

Avoiding the need to wait for completion of the jQuery $.get function

Currently, I am executing a basic jQuery GET request as shown below and it is functioning correctly. $.get("http://test.example.com/do-something", function (data) { console.log("test work done"); }); The GET request is initiated without affecting the ...

Guide to developing and showcasing a proof of concept (PoC) for a Google Chrome vulnerability using Out of Bounds (oob) method

While reading through an article discussing the Proof of Concept of a CVE in Google Chrome (OOB issue), I came across this intriguing code snippet: (https://medium.com/@elniak/cve-2024-4761-exploiting-chromes-javascript-engine-highly-exploited-poc-presente ...

Exploring ways to utilize fetch results within a return statement in React

I have fetched data that is displayed as shown below. Now, I am trying to figure out how to render this fetch in the return statement within the results div. Does anyone have any ideas on how to achieve this? I attempted using a map function because I assu ...

How to position div elements side by side using CSS and center them, even on IE8

UPDATE: I have discovered that implementing Bart's solution is the correct one. With a 264px wide div containing the other divs and their 1px borders, I achieved the desired effect. I have updated my code to include his answer. Thank you, Bart. Once ...

Creating dynamic HTML elements using ngFor within AngularJS allows for increased flexibility and customization on the

I'm a newcomer to Angular 5 and I'm looking to retrieve HTML elements from a .ts file and dynamically add them to an HTML file using ngFor. I attempted to use [innerHtml] for this purpose, but it was not successful and returned [object HTMLSelect ...

SSL causing Wordpress AJAX call to redirect from HTTPS to HTTP

My AJAX request seems to be encountering a redirection issue to HTTP. Despite trying different solutions like adding a trailing slash and changing the URL to "https" as recommended in similar posts, I am still facing rejection. jQuery.ajax({ url: "https:/ ...

Leveraging EJS for embedding Google Maps API on a website

Currently, I am utilizing EJS to display the Google Maps API on a particular webpage. <!DOCTYPE html> <html> <head> <title></title> </head> <body> <div id="map"></div> <script> var ...

Getting to grips with accessing HTML elements within a form

<form name="vesselForm" novalidate> <input type="text" id="owner" name="ownerEdit" required ng-blur="vesselForm.btnCC.attr('value', 'Change Customer')"/> <input type="button" name="btnCC" value="Customer" /> </fo ...