0 votes
in SoSci Survey (dt.) by s140266 (120 points)

Ich möchte in meine Umfrage randomisiert youtube-Videos über die youtube iframes-API für javascript einbinden.
Wie bekomme ich die Randomisierung von SoSciSurvey (Variablen-Kennung RA01, zwei Zettel) in javascript? Nach meinem Verständnis geht das in php mit value("RA01").
Folgende Idee habe ich zusammenkopiert, aber die javascript variable group ist leer:

<div id="player", style="position: fixed; right: 0; bottom: 0; min-width: 100%; min-height: 100%;"></div>

<div id="dom-target" style="display: none;">
    <?php
        $output = value("RA01"); // Again, do some operation, get the output.
        echo htmlspecialchars($output); /* You have to escape because the result
                                           will not be valid HTML otherwise. */
    ?>
</div>

<script type="text/javascript">
    var player, iframe, videoID;

    var div = document.getElementById("dom-target")
    var group = div.textContent;
    window.alert(group);
    if (group == 1){
      videoID = 'hklFLONVXws';
    } else {
      videoID = 'uu_h8l4cYnU';
    }
    

    // 2. This code loads the IFrame Player API code asynchronously.
    var tag = document.createElement('script');

    tag.src = "https://www.youtube.com/iframe_api";
    var firstScriptTag = document.getElementsByTagName('script')[0];
    firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

    // 3. This function creates an <iframe> (and YouTube player)
    //    after the API code downloads.
    function onYouTubeIframeAPIReady() {
        player = new YT.Player('player', {
          id: "AutonomesFahren", 
          height: '360',
          width: '640',
          videoId: videoID,
          events: {
            'onReady': onPlayerReady,
            'onStateChange': onPlayerStateChange
          },
          playerVars:{
            disablekb: 1,
            controls: 0,
            showinfo: 1
          },
        });
      }

      // 4. The API will call this function when the video player is ready.
...

Orientiert habe ich mich an https://stackoverflow.com/questions/23740548/how-do-i-pass-variables-and-data-from-php-to-javascript und https://developers.google.com/youtube/iframe_api_reference?hl=de.

Vielen Dank für die Unterstützung!

by s140266 (120 points)
Ich habe mittlerweile eine (unschöne) Lösung gefunden: In php wird die zufällige Gruppe abgefragt und entsprechende Seiten mit der anderen Bedingung übersprungen.

1 Answer

0 votes
by SoSci Survey (304k points)

Am einfachsten schreiben Sie die URL (etwas anderes ändert sich ja nicht) mit replace() in einen Platzhalter und verwenden diesen dann in Ihrem HTML-Code.

Siehe auch: Stimulus -> Stimulus randomisieren

Willkommen im Online-Support von SoSci Survey.

Hier bekommen Sie schnelle und fundierte Antworten von anderen Projektleitern und direkt von SoSci Survey.

→ Eine Frage stellen


Welcome to the SoSci Survey online support.

Simply ask a question to quickly get answers from other professionals, and directly from SoSci Survey.

→ Ask a Question

...