2

I have an input function that accepts an iframe embed URL from the user is there any way to get the src from the input. For example: if user inputted <iframe width="1263" height="480" src="https://www.youtube.com/embed/KWFKabzKSxw" title="YouTube video player" frameborder="0" allowfullscreen></iframe> i want to extract the src that would be https://www.youtube.com/embed/KWFKabzKSxw and assign it to a new var. My current code is attached below. Any help is appreciated. Thanks in advance.

function input() {
  globalThis.urlInput = "";
  urlInput = prompt("Enter embed URL:");
  if (urlInput == "") {
    alert("No URL Inputted");
  } else {
    rename();
  }
}

/*something like this but this wont work because urlInput is not an Id*/
function rename() {
  var newUrl = document.getElementById(urlInput).src;
}
<button onclick="input()">Input1</button>

0

3 Answers 3

1

Just convert the input text to html using the DOMParser() API, retrieve the src from it and then assign the url to a variable say, x like this:

function input() {
  globalThis.urlInput = "";
  urlInput = prompt("Enter embed URL:");
  if (urlInput == "") {
    alert("No URL Inputted");
  } else {
    rename();
  }
}

function rename() {
  const parser = new DOMParser();
  const x = parser.parseFromString(urlInput, "text/html").body.firstChild.src; //convert the retrieved input text to HTML and retrieve the src attribute value from it
  alert(x);
}
<button onclick="input()">Input1</button>

Sign up to request clarification or add additional context in comments.

2 Comments

this won't work I am trying to get the src straight from the user input @AndrewL64
@anon10023108 Oh, you mean retrieve the src from the prompt? Hold on.
1

function input() {
  globalThis.urlInput = "";
  urlInput = prompt("Enter embed URL:");
  $('#input').html(urlInput);
  $('#input').hide();
  console.log($('#input iframe').attr('src'));
  if (urlInput == "") {
    alert("No URL Inputted");
  } else {
    rename();
  }
}

/*something like this but this wont work because urlInput is not an Id*/
function rename() {
  //var newUrl = document.getElementById(urlInput).src;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button onclick="input()">Input1</button>
<div id=input type="hidden"></div>

Comments

1

I have created a hidden DOM and added into body and get the URL and delete the DOM after saving the src from Iframe from prompt.

function input() {
  globalThis.urlInput = "";
  urlInput = prompt("Enter embed URL:");
  if (urlInput == "") {
    alert("No URL Inputted");
  } else {
    rename();
  }
}

/*something like this but this wont work because urlInput is not an Id*/
function rename() {
var input = document.createElement('div');
input.style.display = 'hidden';
input.id = 'tempInput'

document.body.appendChild(input); 
input.innerHTML = urlInput;

var URL = document.querySelector('#tempInput iframe').src;
  console.log(document.querySelector('#tempInput iframe').src);
  
  document.getElementById("tempInput").remove();
}
<button onclick="input()">Input1</button>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.