canacast
canacast

Reputation: 241

Change or Remove one character in an element src

I have an iframe where I need to change one character.

EDIT: adding lots of additional code as this is more difficult that I first thought.

<iframe href="https://derpxample.com/">
<video id="post_html5_api" class="vjs-tech" loop="" preload="auto" src="https://derpxample.com/v/berp.mp4">
            <source src="https://derpxample.com/v/berp.mp4" type="video/mp4">
</video>
</iframe>

I'm just trying change the url protocol from https to http as simply as possible. The content source will not be changed as it's generated automagically. Thus, the following:

$('iframe').each(function(){
      var href = $('iframe').attr('href');
      $('iframe').attr('src',href).attr('class','postVine');
      $('video').attr('src', function(i, s) { 
      return s.replace("https", "http");
      });                 
});

Upvotes: 1

Views: 268

Answers (5)

Gregism
Gregism

Reputation: 392

Perhaps this will work for you:

$('video').attr('src', $('video').attr('src').replace("https", "http"));

Edit: For the iFrame you might try something like this:

video = $('iframe').contents().find('video');
$(video).attr('src', $(video).attr('src').replace("https", "http"));

Upvotes: 1

jcsanyi
jcsanyi

Reputation: 8174

I suspect the key to your question is that you mention an <iframe>.

If you're trying to access or change the contents of an iframe, you can only do that if it's loaded from the same host as your parent page.

See this question for more details: Changing the contents of an iframe

Upvotes: 1

jcsanyi
jcsanyi

Reputation: 8174

How about something like this?

var video = document.getElementById('videoID');
video.src = video.src.replace(/^https:/, 'http:');

This does not assume that the src always starts with https:, and does not break if https occurs elsewhere in the URL.

Upvotes: 1

RienNeVaPlu͢s
RienNeVaPlu͢s

Reputation: 7632

videoElem.src = 'http'+videoElem.src.substr(5);

Upvotes: 1

mike
mike

Reputation: 859

Use some like this:

 var element = document.getElementById('my-video');
 element.src = element.str.replace('https', 'http');

Element:

 <video id='my-video' src = 'https://example... > 

Upvotes: 1

Related Questions