Displaying a form within an iframe

I am trying to embed a form within an iframe that will load on a random page when the user clicks a bookmarklet.

Below is the code snippet I have written so far:

    loginForm = document.createElement("form");
    loginForm.setAttribute("method","Post");
    loginForm.setAttribute("action","http://devserver:8000/action/");
    parameters = {};
    parameters['url'] = parent.window.location;

    for(var key in parameters)
    {
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute('type',"hidden");
        hiddenField.setAttribute('name',key);
        hiddenField.setAttribute('value',parameters[key]);
        loginForm.appendChild(hiddenField);
    }

    loginIFrame = document.createElement('iframe');
    loginIFrame.src = "about:blank";
    loginIFrame.appendChild(loginForm);
    loginIFrame.style.top = "0px";
    loginIFrame.style.position='fixed';
    loginIFrame.style.display = 'block';
    loginIFrame.style.zIndex = 100;
    loginIFrame.style.border = "solid #48D236 10px";
    loginIFrame.height = "25px";
    loginIFrame.width = "100%";
    loginIFrame.style.border = 0;
    loginIFrame.id = "loginFrame";
    loginIFrame.name = "loginFrame";
    usernameField = document.createElement("input");
    usernameField.type = "text";
    usernameField.size = 8;
    usernameField.name = "usernameField";
    usernameField.id = "usernameField";

    passwordField = document.createElement("input");
    passwordField.type = "password";
    passwordField.size = 8;
    passwordField.name = "passwordField";
    passwordField.id = "passwordField";


    submitButton.style.position='fixed';
    submitButton.style.top = "60px";
    submitButton.type = "button";
    submitButton.value = "Submit";
    submitButton.onclick = function(){loginUser();};*/
    b.style.position="relative";


    addToBody(loginIFrame);
    loginForm.submit();

Problem arises as the entire page reloads upon submission (last line of code) instead of just the iframe. Any suggestions?

Thank you in advance

Answer №1

Consider utilizing

loginIFrame.contentWindow.document
to add the form instead of loginIFrame.appendChild.

You may also need to ensure that your form is added after the iframe has been inserted into the page, as the contentWindow property might not be accessible otherwise.

It seems like the form is not being correctly added to the iframe due to the use of appendChild. Manipulating it this way could lead to loading a specified URL (e.g., about:blank).

Edit: Additionally, try adding

loginForm.setAttribute("target", "loginFrame");

Below is a snippet similar to what I've tested and found success with:

 <!-- Your code goes here -->
  • Christian

Answer №2

Consider setting the form target to the iFrame. It seems like the elements you're generating within the "parent" page context might be targeting the main page as a destination (even though it's embedded).

UPDATE Another option is to create an element from the iFrame itself after generating it in order to maintain context.

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

Making a standard AJAX request using jQuery without including the XHR header

I am looking to make an ajax-type request using the following headers (or something similar): GET example.com/ajaxapi/article/1 HTTP/1.1 Host: localhost Accept: application/hal+json Cache-Control: no-cache The key aspect here is to accomplish this withou ...

What is the best way to pass a module from the controller to a Jade/Pug template and then use it as an argument in an event handler within the template?

I passed the module 'naija' to a template from the controller in this way: res.render('testing', {title:"filter setting page", nigeria:naija }); The func ...

When trying to implement a dark/light theme, CSS variables may not function properly on the body tag

Currently, I am in the process of developing two themes (light and dark) for my React website. I have defined color variables for each theme in the main CSS file as shown below: #light{ --color-bg: #4e4f50; --color-bg-variant: #746c70; --color-primary: #e2 ...

Could use some assistance in developing a custom jQuery code

Assistance Needed with jQuery Script Creation <div>Base List</div> <div id="baseSection"> <ul class="selectable"> <li id="a">1</li> <li id="b">2</li> <li id="c">3</li> ...

Leveraging PHP, AJAX, and MySQL within the CodeIgniter framework

I am currently venturing into the world of web development using the codeigniter PHP framework. My current hurdle involves a select dropdown on a page, populated with unique IDs such as 1, 2, 3 from the database. What I aim to achieve is, when a value is s ...

What is the best way to eliminate excess white space on the right side in WordPress for a mobile perspective?

Is there a quick way to identify which element has shifted beyond the border? It seems like there is excess margin. How can I pinpoint the issue? The link to the broken page on mobile is I applied this style * {border: 2px solid red;} and no elements shif ...

Troubleshooting Issue: MVC 5 validation messages not displaying when using Ajax.BeginForm

Having recently delved into MVC 5, I've encountered some issues that have left me stumped despite my best efforts at troubleshooting. Specifically, my struggle lies in creating a validation mechanism for a user and password list to ensure that all fie ...

Error encountered with Jquery script: "Unauthorized access"

I'm currently working on a script that involves opening a child window, disabling the parent window, and then re-enabling the parent once the child window is closed. Here's the code snippet: function OpenChild() { lockOpportunity(); if (Clinical ...

Spinning html/css content using JavaScript

Greetings! I am currently working on a demo site using just HTML, CSS, and JavaScript. Typically, I would use Ruby and render partials to handle this issue, but I wanted to challenge myself with JavaScript practice. So far, I have created a code block that ...

"Combining background images with javascript can result in displaying visual elements

Hello! I am in need of assistance with a CSS + Javascript fog effect that I have developed. It is functioning properly on Firefox, Opera, and Chrome but encountering issues on IE and Edge browsers. The effect involves moving two background images within a ...

Error Message: Error Code 2. I am encountering an issue with the bot expecting either undefined or null and receiving a string primitive instead. Any

As I delved into creating a music bot with support for slash commands, everything seemed to be going smoothly when embedding a URL to a single song. However, the trouble began when attempting to include a link to a playlist, resulting in two perplexing err ...

Error: Attempting to use the 'append' method on an object that does not support the FormData interface

$(document).on('submit','#form_pem', function(event){ event.preventDefault(); var kode = $('#kode').val(); var name = $('#name').val; var price = $('#price'). ...

Unable to ajax post on IE10

My struggles with getting ajax to function in IE10 continue. Despite various attempts and approaches, the result is always the same - an empty string. <html> <head> <script type="text/javascript" src="jquery-1-9-1.js"></script&g ...

The URL routing in Vue-router seems to be incorrect as it directs to the wrong page. To fix this issue, make sure to include a '#' before the

Welcome to my website! Check out my home page here: https://i.sstatic.net/znfq0.jpg If you're interested in reading my blog, visit the blog page here: https://i.sstatic.net/oiYSY.png I've built this site using vue3. Here's a snippet of th ...

Using Jquery to toggle columns based on their index and custom attribute

My table has two rows in the header: the first row with colspan and the second row containing headers. You can see an image below for reference. https://i.sstatic.net/qwSBL.png With a jQuery function, I extract all the table cell values from the second h ...

Obtain a specific text value as a variable using Google Tag Manager

In my quest to extract the dynamic "sku" value from GTM, I am faced with a challenge. This particular value is unique for each product page. Below is an example of how the code appears on the page: <script type="application/ld+json"> { "@context" ...

The "View" function is not functioning as expected

I am trying to navigate to another view within the same controller from a controller function. Below is the code I have implemented: In the view : $.ajax({ type: "POST", url: '@Url.Action("CreateEBulletinPdf","EBulletin")', contentT ...

Issue: nodebuffer is incompatible with this platform

Currently, I am attempting to utilize the Shpjs package in order to import a Shape file onto a Leaflet map. According to the Shpjs documentation: shpjs Below is the code snippet I am working with: const [geoData, setGeoData] = useState(null); //stat ...

Retrieving data from an AJAX dialog with the use of jQuery and ASP.NET

After spending more than a day on this and reading countless posts on the topic, I am still unable to make it work. My goal is to retrieve a value from a textbox (starting with a textbox for testing) in a javascript pop-up in asp.net using js / ajax. Below ...

What is the best approach for iterating through the creation of Objects?

Here is a simplified version of the current code, with fewer rows and properties. var row1 = new Object(); var row2 = new Object(); var row3 = new Object(); var row4 = new Object(); var row5 = new Object(); var row6 = new Object(); var row7 = new Object() ...