Using dynamic JavaScript appending with the location.href attribute and the ajax-cross-domain.com script for seamless integration

Once I assign this code to load the function on window.onload event: window.onload = initfunction;

I am looking to add an AJAX cross domain script to the header:

function initfunction() {
 var dh = document.getElementsByTagName('head')[0];
 var script = null;
 script = document.createElement('script');
 script.setAttribute('src', 'http://whatever.com/cgi-bin/ACD/ACD.js?' + location.href);
 script.setAttribute('type', 'text/javascript');
 dh.appendChild(script);
  }

The script appears to be added with the correct domain name, but Firebug keeps showing "Failed to load source" error. It works when a fixed URL is entered in the src attribute! For example:

script.setAttribute('src', 'http://whatever.com/cgi-bin/ACD/ACD.js?http://google.com');

Any suggestions or insights?

Answer №1

When referring to the script on ajax-cross-domain.com, wouldn't it make more sense to have it like this:

script.setAttribute('src', 'http://whatever.com/cgi-bin/ACD/ACD.js?uri=('+encodeURIComponent(location.href)+')');

Answer №2

Check out this simplified code snippet designed for testing purposes. Simply insert it as an onload function or within a script tag in the webpage header. Watch as the page continuously loads...

var header = document.getElementsByTagName('head')[0];
if(!header)
{
  //Web page does not have a "head"
  var head = document.createElement('head');
  document.appendChild(head);
}
var newScript = null;
newScript = document.createElement('script');
newScript.setAttribute('src', 'http://domain.com/cgi-bin/ACD/ACD.js?' + location.href);
newScript.setAttribute('type', 'text/javascript');
header.appendChild(newScript);

Answer №3

Aha! I have discovered the solution. The issue was not with the "location.href" command, but rather a rule within our firewall that blocked GET requests to our own server. This caused the script to time out.

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

In what way and where does Selenium convert Java or other high-level programming language commands into native JavaScript in order to interact with the browser?

While Javascript is essential for interacting with browser elements, Selenium offers APIs for various high-level programming languages such as Java and C#. How does Selenium handle these java commands when the code is not written in javascript? Is there ...

Guide on verifying if the request is an AJAX request using PHP

Is there a way to verify on the server-side if a request made to my PHP page is an AJAX request or not? I have come across two methods to achieve this: First method: including a GET parameter in the request to indicate that it is an AJAX request (=mypage ...

How can I prevent my code from resetting every time a state update occurs?

https://i.sstatic.net/snSGl.pngI've coded a program that creates a 10x10 grid with 5 boxes of varying sizes. When you click on a box, an x is added to its content and the turn state is set to false. The issue arises when the code re-runs after each s ...

Unexpected results occur when accessing data in $uibModal.open() within Angular JS causing it to

Here is the code snippet that is causing the issue of returning undefined items in AngularJS: App.js code console.log('single page application script is working'); var myApp = angular.module("demoApp", ["ngRoute", 'ui.bootstrap',&apos ...

Enhance User Experience with React JS Multi-Select Dropdown Feature

I am dealing with 4 select dropdowns. The values selected should not be available in the remaining dropdown lists. Here is an overview of my state: this.state = { selectedDropdownArray: {}, dropdownArray: ['select', '1', &apos ...

Discover the technique for determining the XPath location of an element through Javascript

Imagine you have a lengthy HTML file filled with various tags, much like the layout of Stack Overflow. If you were to click on a specific element on the page, what would the Javascript function look like to determine the simplest XPath that points to that ...

Create a repeating function that will show an image based on the specific class assigned to each individual element

Can someone help me create a function that automatically applies to all divs with a specific class on my document? I want the function to check for the presence of another class within those divs and display an image accordingly. document.getElementsByCla ...

The beta version of Angular 2.0 introduces the BrowserDomAdapter for easy access to the DOM

I have a Component in Angular 2.0 that is attempting to utilize the DOM Adapter API from Angular's BrowserDomAdapter documentation. The initialization of this DomAdapter can be found here. However, I am uncertain about whether the Dom Adapter needs t ...

PHP encountered an issue when retrieving a value from a URL and passing it to a JavaScript variable

How can I pass a PHP variable from the URL using $_REQUEST to JavaScript in order to send it through Ajax? Here is the code snippet at the top of my page: <?php include_once('../php/connection.php'); include_once('../php/getDiagnosi ...

What is the purpose of passing data into the 'success' function of an AJAX request?

Recently, I embarked on a journey to learn jQuery and AJAX, but I found myself tangled in confusion when it came to AJAX requests. To gain practice, I decided to create a simple TodoApp using Node, jQuery, and Bootstrap. While I managed to grasp GET and P ...

Rails not receiving JSON data

I am attempting a straightforward ajax call in Rails 4, but encountering issues with retrieving the json response. Below is the script I'm working with: $(document).on "submit", "form[name=upvote-form]", -> form = $(this) $.post "/vote", $(th ...

Sending a response in the catch block based on conditions

I am currently working on finding the correct method to handle a potential bad Fetch response. My goal is to immediately send a 500 response and halt the code execution if the Fetch response is not okay. However, if the response is acceptable, I need to ...

When using the Google API load callback, the Angular(4) router does not actually replace routes; instead, it stacks them up each time

I've been attempting to implement Google Authentication in my Angular 4 application. I have successfully loaded the Google platform.js and api.js in my index.html file. When I click on the login button, this is what I have coded: gapi.load('auth ...

What is the best way to pass an email as a Laravel parameter within a function using Angular?

I'm currently working on a feature that allows users to delete their account only if the input field matches their email address. However, I encountered an error: Error: $parse:lexerr Lexer Error when attempting to set this up. Here is my HTML code: ...

Issue: Module 'curl' is not located at Function.Module._resolveFilename (module.js:489:15)

After installing node.js using the Windows installer, I noticed that the folder structure created was C:\Program Files\nodejs\node_modules\npm\node_modules. It seems like all the module folders are in the last node_modules director ...

Unable to resize images in Firefox

Why is it that resizing doesn't seem to work in Firefox but functions fine in Internet Explorer? I'm struggling to find a way to make it consistent across all browsers. My goal is to resize the width to 800 and height to 475, disable maximizing t ...

Align the camera to be perpendicular with the ground

Ensuring that my fly camera always remains parallel to the ground is crucial to me. Specifically, I need my camera's right vector to always be (1,0,0). To achieve this, I have developed a customized Camera Controller in three.js that simulates unique ...

I am currently utilizing Ajax to trigger the forgot password button, which then sends an email to a specific email address through CodeIgniter

When using ajax to click the forget button and send an email to a particular email address, I am getting an error "Fail". Can anyone help me figure out what the problem might be? Below you can see the form and controller code: Form <div class="field-w ...

Using Javascript to automatically submit a form when the enter key is pressed

Can anyone help me with a password form issue? I want the enter key to trigger the submit button but it's not working for me. I understand that the password can be viewed in the source code, this is just for practice purposes. <!DOCTYPE html> & ...

Implementing styles from a constant file in React Native: A simple guide

In my file register.js, I have a UI component. import CustomHeader from '../components/Header'; ... static navigationOptions = ({navigation, navigation: { state } }) => { return { title: '', headerSty ...