Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 4218
Alex Hales
  • 0
Alex HalesTeacher
Asked: June 3, 20222022-06-03T15:07:24+00:00 2022-06-03T15:07:24+00:00

javascript – How to first check in Ajax if the link of the “video” returned by the JSON of the PHP Code is of HTML “oEmbed” type?

  • 0

[ad_1]

Here is my PHP code from the ajax/setVideoBroken.php file which returns a Json:

if(isset($_POST["src"])) {
    $query = $con->prepare("UPDATE videos SET broken = 1 WHERE videoLink=:src");
    $query->bindValue(":src", $_POST["src"]);
    $query->execute();
    
    $response = ['success' => false];
    $videoQuery = $con->prepare("SELECT * FROM videos WHERE videoLink=:src");
    $videoQuery->bindValue(":src", $_POST["src"]);
    $videoQuery->execute();
    while($row = $videoQuery->fetch(PDO::FETCH_ASSOC)) {
        $response['success'] = true;
        $response = [
            'success' => true,
            'video_link' => $row["videoLink"],
            'thumbnail' => $row["thumbnail"],
            'video_type' => 'video/' . $row["video_extension"],
        ];  
    }
    header('Content-Type: application/json; charset=utf-8');
    echo json_encode($response);
    exit;
} else {
    echo "No src passed to page";
}

Which Json returned in PHP above, is fetched in Ajax (JavaScript) to display the video_link video link in the source tag which itself is included in the video tag:

function loadVideo(src, className) {
    
    var video = $('<video id="player" playsinline controls><source /></video>');

    video.on("load", function() {
        $("." + className + " a").append(video);

        clearTimeout(timer);

        timer = setTimeout(function() {
            $(".imageResults").masonry();
        }, 500);

    });

    video.on("error", function() {
        
        $("." + className).remove();

        $.post("ajax/setVideoBroken.php", {src: src});

    });
    
    $.post("ajax/setVideoBroken.php", {src: src}, function(response) {
        if(response.success) {
            $('video', $video).attr('data-poster', response.thumbnail);
            $('source', $video)
                .attr('src', response.video_link)
                .attr('type', response.video_type);
        }
    });

}

I want to specify that response.video_link can contain simple video URLs (like: https://website.com/video.mp4) or oEmbed HTML code like that of Youtube (like: <iframe src="https://www.youtube.com/embed/4YCLRpKY1cs?feature=oembed"></iframe>)

So, how to check in JavaScript code:

  • If response.video_link (the video_link response of the Json returned by the PHP Code) begins with < which represents the opening sign of the HTML tag ???

  • And if Yes (video_link begins with the < sign, it means that video_link is equivalent to an oEmbed type HTML code like that of Youtube), give the value of this HTML code directly to the var video variable.

  • If not, we must to pass to the variable var video the code <video id="player" playsinline controls><source /></video> so as to add to the tag source, the link of the video what the JSON response contains: video_link.

I really need your help.

[ad_2]

  • 0 0 Answers
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 8037 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 1840 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 1918 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.