I would like to put it into a function and don’t know how to do it. This is what I’ve got. The getplaylist part works but that’s all. I don’t know how to incorporate the rest of it into the function; but I’ve written it out so that you can see what I want it to.
function Play(){
var file = location.href.substring(location.href.lastIndexOf('/')+1);
var fileName =file.split('.',1);
var getplaylist = [fileName] + "/" + 'playlist.m3u';
'href=[getplaylist] onclick="parent.document.getElementById
("iframe1").contentWindow.PlayM3u(this.href);return false"';
}
<a href="music/playlist.m3u" class="playerlink">Play this playlist</a>
var anchs = document.getElementsByTagName(‘a’);
for (var i = 0, j = anchs.length; i < j; i++) {
if (anchs[i].className !== ‘playerlink’) continue;
anchs[i].onclick = function() {
parent.document.getElementById(‘iframe1’).contentWindow.PlayM3u(this.href);
}
}
I don't understand what the getplaylist stuff is for, as everything seems to depend on the href attribute of the link. If you want to set the href with javascript, you need a way to identify the correct links somehow.
It's all a bit nebulous at the moment.
Thanks for the reply. The html files that will link to this function from an external js have the same name as the subdirectories where the playlist files are. So the getplaylist part gets the music/playlist.m3u based on what html file is currently open in the right iframe.for instance if bandofhorses.html is open in the right iframe, the getplaylist will get bandofhorses/playlist.m3u. That part works.
My problem is I don’t know how to make it into a complete function that would do this:
Sorry if I wasn’t clear about that. I appreciate your help. I had the link up there to show it as it was and working correctly. I was only using the link to get it to work. My goal was to put it into a function. Unfortunately I can’t get the script you posted to work.
OK, I understand, but the [getplaylist] bit is unclear. Where is the location going to come from? What is going to replace [getplaylist]?? If you want JavaScript to do it based on the window.location, that’s fine, but you need to be able to target the link, with an ID or something like that.
OK, that’s all fine, but if the function is going to do this, you still need to target the link itself somehow. This is especially necessary if you’re going to have more than one link. If you only have one link, it’s simple enough:
<a href="#" id="playlist_link">test link</a>
var playlist_link = document.getElementById('playlist_link');
var file = location.href.substring(location.href.lastIndexOf('/')+1);
var fileName =file.split('.',1);
var getplaylist = fileName + "/" + 'playlist.m3u';
playlist_link.href = getplaylist;
playlist_link.onclick = function() {
parent.document.getElementById('iframe1').contentWindow.PlayM3u(this.href);
return false;
}
the location is going to come from the title of the html page that is calling the function.
var file = location.href.substring(location.href.lastIndexOf('/')+1);
var fileName =file.split('.',1);
var getplaylist = [fileName] + "/" + 'playlist.m3u'
the first line gets the name of the current html. for instance bandofhorses.html
the second line splits it in half leaving just the name bandofhorses
the getplaylist puts it together and makes the path to my subdirectory bandofhorses/playlist.m3u
If I add an alert it pops up bandofhorses/playlist.m3u