Home » vue » How Can I Check If String Starts With iFrame?

How Can I Check If String Starts With iFrame?

Posted by: admin November 26, 2021 Leave a comment

Questions:

We are building a newsfeed app where some of the posts contain iframed youtube videos and others do not. In order for the videos to be responsive, we have wrapped div in a div with a class of “video container”. However, we only want that class to be active when the data string starts with .

This is for items in a v-for loop.

Using vuejs, how can we check to see if the data string includes “iframe”?

<v-card-text class="pt-1 pb-0">
      <div :class="{'video-container': 
(videoCheck(props.item.content) )}">
   <p v-html="props.item.content"></p>
      </div>
</v-card-text>

and the method:

videoCheck: function(item){
  var substring = "<iframe>"
  return item.includes(substring)
},
Answers:

To my knowledge, the most backwards compatible solution is to use indexOf:

return item.indexOf(substring) == 0;

There is also startsWith in all latest browsers:

return item.startsWith(substring);