Creating a script to open multiple URLs in HTML and JavaScript

Currently, I am working on creating a multiple URL opener using HTML and JavaScript. However, I have encountered an issue where HTTP links are opening fine but HTTPS links are not. Can someone provide assistance with this problem? Below is the code snippet.

function open_all() {
  var urls = document.getElementById("list_urls").value;
  var urls = urls.split('\n');
  var totalno = urls.length;
  var s;
  for (var i = 0; i < totalno; i++) {
    s = urls[i];
    if (s) {
      if (s.substr(0, 7) != 'http://') {
        s = 'http://' + s;
      }
      window.open(s);
    }
  }
  return false;
}
<form method="post" action="">
  <br />
  <textarea name="list_urls" id="list_urls" cols="60" rows="20"></textarea>
  <br />
  <br />
  <input value="Open URLs" class="submit" type="button" onClick="open_all();">
  <br />
  <input type="reset" value="Reset!">
  <br/>
</form>

Any help would be greatly appreciated!

Answer №1

The problem arises from the current code where http:// is added to URLs that start with https://. To resolve this, the logic needs to be adjusted to check for both https:// and http:// at the beginning of the URL. Additionally, the logic can be streamlined by using trim() to ensure there are no extra spaces in the line. See the revised code below:

function open_all() {
    var urls = document.getElementById("list_urls").value.split('\n');
    for (var i = 0; i < urls.length; i++) {
        var url = urls[i];
        if (url.trim()) {
            if (s.substr(0,7) != 'http://' && s.substr(0,8) != 'https://') 
                url = 'http://' + url;
            window.open(url);
        }
    }
    return false;
}

It's worth noting that your browser's popup blocker might interfere with opening multiple windows rapidly.

Answer №2

Ensure to include some additional logic in your if statement for the presence of "https-"

if(s.substr(0,7)!='http://' && s.substr(0,7)!='https:/')

:)

Answer №3

give this a shot

  function open_all_tabs() {
        debugger;
        var links = document.getElementById("list_links").value;
        var links_array = links.split('\n');
        var total_links = links_array.length;
        var current_link;
        for (var j = 0; j < total_links; j++) {
            current_link = links_array[j];
            if (current_link) {
                if (current_link.substr(0, 7) != 'http://' && current_link.substr(0, 8) != 'https://')
                    current_link = 'http://' + current_link;
                window.open(current_link);
            }
        }
        return false;
    }

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

Incorporating external function from script into Vue component

Currently, I am attempting to retrieve an external JavaScript file that contains a useful helper function which I want to implement in my Vue component. My goal is to make use of resources like and https://www.npmjs.com/package/vue-plugin-load-script. Thi ...

Tips for creating responsive Math Latex

Check out this link to my website page. Also, take a look at this other link. While using Bootstrap, I've noticed that all content is responsive except for the equations in the first link and the Matrix multiplication example in the second link towar ...

Reset the queue for the slideDown animation using jQuery

I am experiencing an issue with my code where multiple animation effects are running simultaneously. Specifically, when I hover over navbarli1 and then move to another li element with the same navbarli1 class, the slide-down effect is not working as expect ...

The communication between a Firefox XUL extension and a webpage

Currently developing a Firefox XUL extension and in need of incorporating interaction between the web page and the extension. For instance, whenever a link is clicked on the page, I would like to trigger a function within the XUL extension. Is there any k ...

Is it possible to refactor this forwardRef so that it can be easily reused in a function?

Currently, I am in the process of transitioning my application to Material UI V4 and facing challenges with moving my react router Link components into forwardRef wrapped components when setting the 'to' prop programmatically. The code below doe ...

Is there a discrepancy in performance when running a function on an individual element versus a group of elements within jQuery?

Imagine having the choice between applying a function to an individual DOM element or a list of them: For Individual Elements: $('#element1').click(function () { $(this).hide(); return false; }); $('#element2').click(functi ...

Drawing on Canvas with Html5, shifting canvas results in significant issues

I've been working on developing an HTML5 drawing app, and while I have all the functionality sorted out, I'm facing challenges during the design phase. My main issue is centered around trying to make everything look visually appealing. Specifical ...

Creating Structured Lists with HTML Columns

Can you assist me in achieving a single column layout for all the bullet points, as I would like to experiment with various designs? Below is what my current display looks like and how I envision it to be presented: https://i.stack.imgur.com/NhMCC.jpg Pl ...

Utilizing PHP to dynamically load HTML forms and leveraging JQuery for form submissions

I am currently facing a dilemma that I am unsure how to approach. It seems that JQuery requires unique ID's in order to be called in the document ready function. I use PHP to read my MySQL table and print out HTML forms, each with a button that adds a ...

What is the best way to incorporate a unique box shadow onto my svg image?

While I've seen a similar question here: How to add box-shadow to a svg path circle shape, I am struggling to grasp how the transition from box shadow characteristics to SVG drop shadow characteristics was achieved. (Especially given that SVG Drop sha ...

Creating dynamic SQL queries for bulk inserting data into Postgres using Vercel

Can anyone help me with creating an SQL bulk insert query using the sql helper from @vercel/postgres? I have a array of objects with different property types (number, string, date) and need to dynamically build the query. Simply creating a string and passi ...

Error handling middleware delivering a response promptly

Issue with my express application: Even after reaching the error middleware, the second middleware function is still being executed after res.json. As per the documentation: The response object (res) methods mentioned below can send a response to the cl ...

How I am able to access this.state in React without the need for binding or arrow functions

Understanding the concept of arrow functions inheriting the parent context is crucial in React development. Consider this React component example: import React, { Component } from 'react'; import { View, Text } from 'react-native'; i ...

Revolutionizing Zen Cart with slimMenu

Is there a smarter person out there who can help me understand why the slimMenu is breaking when I add these files to the bottom of the page? <link href="includes/templates/westminster_new/css/age-verification.css" rel="stylesheet"> <script src=" ...

Struggling to make the CSS :not() selector function correctly as needed

I've been struggling to make the CSS :not() selector work in a specific way and despite searching for solutions, I haven't been successful. I came across some helpful resources like this page, but I couldn't figure it out on my own. Hopefull ...

iCheck jQuery Callback

I'm using Jquery iCheck to style input fields and I have encountered a problem with handling the callback from iCheck. What I need is to retrieve all checked input values within a container that has a specific class name. http://jsfiddle.net/lgtsfidd ...

Validation of hidden fields in MVC architectureUsing the MVC pattern to

Depending on the selections made in the dropdown menu, certain fields will appear and disappear on the page. For example: <section> @Html.LabelFor(model => model.AuctionTypeId) <div> @Html.DropDownList("AuctionTypeI ...

The document could not be read by Jackson due to an unrecognized token 'contactForm', which was expecting either 'true', 'false', or 'null'

When attempting to send a POST request using JQuery to a Spring Controller, an error keeps popping up from JQuery. An issue arises with the following message: "Could not read document: Unrecognized token 'contactForm': was expecting ('true& ...

Validating uploaded files in Javascript and handling server upload operations

I'm having a small issue with a webpage I am creating. Essentially, I am looking to validate whether a user has selected a file and then upload it to the server. I understand this can be done using JavaScript: if(document.getElementById("uploadBox"). ...

An issue arose during the installation of nodemon and jest, about errors with versions and a pes

Currently, I am facing an issue while trying to set up jest and nodemon for my nodejs project. My development environment includes vscode, npm version 6.13.7, and node version 13.8.0. Whenever I try to install nodemon via the command line, the console disp ...