I have the following code in abc.jsp
<%
out.write("<script type=\"text/javascript\" src=\"scripts/test.js\"></script>");
out.write("Here is filename"+sfl);
out.write(""
+ "<input type=\"button\" id=\"playBt\" value=\"Play Now\" onClick=\"playSound()\" >"
+ "<audio id=\"sound\" preload=\"auto\">"
+ "<source src=\"music.ogg\" type=\"audio/ogg\" />"
+ "<source src=\"music.mp3\" type=\"audio/mpeg\" />"
+ "Your browser does not support the audio element."
+ "</audio>"
);
out.write("");
%>
Then content above was brought in in a frame in index.jsp
<iframe id='bgframe' style='display:compact;' src='abc.jsp' width="400" height="200"></iframe>
This JavaScript function is now used - test.js
function playSound() {
var frameRef = document.getElementById('bgframe');
var sound = frameRef.contentWindow.document.getElementById'sound');
//var sound = frameRef.contentDocument.document.getElementById('sound');
sound.play();
}
The problem is that, the sound does not play when I click on play button.
On the JavaScript console, the system returns null as value of frameRef
.
Also, I get the following error depending on whether I use contentDocument
or contentWindow
.
Uncaught TypeError: Cannot read property 'contentDocument'
Uncaught TypeError: Cannot read property 'contentWindow'
I've implemented different solutions but the uncaught typedef error remains.
Please what am I doing wrong here and how can I fix it? Why can't I access elements on frame id=bgframe
?