Will this hash code change the URL location on randomize? - javascript

still practicing javascript.
Here is a JSBIN of a site that I have made that I can't seem to figure out how to make it so the URL will change once the video is clicked or randomized.
(page load/refresh and click both trigger this)
http://jsbin.com/vesebu/1/edit?html,js,output
The page randomizes on load and page click with an array of videos.
What I am trying to figure out is how to change the URL by appending an ID to the end of it. ie: mysite.com/6530 <-- last four digits representing the ID of the video. This will make the site shareable and is what I am trying to figure out how people are able to do this!
I have tried reading documentation on history.js, html5 pushstate as well as window.location hash and nothing seems to work.
M = function(d) {
function a() {
var a;
a = window.location.hash;
/^#\d+$/.test(a) && (window.location.href = "/" + a.slice(1));
/^#!\d+$/.test(a) && (window.location.href = "/" + a.slice(2));
f
}
y(a, d);
return a
}(n);
I was suggested this code by a friend but can't seem to make sense of it, especially with the jsbin I have. I'm completely lost here on how to do this, and have really tried reading up on it.
any help at all is definitely appreciated!
Thanks guys

Your hashes may be like this:
#/2134/23423/
#2134/23423/
result:
windows.location = /2134/23423/;
then do something else.......
Hope it can help you ~

Related

Javascript get updated link in real time

I'm new to javascript and I've created a kinda successful extension on chrome for dubtrack I've been trying to figure out for quite awhile how to make my injected script run in real time and grab the latest youtube music video url any help would be much appreciated my extension is very basic and it's not for profit I just made it to play around with javascript and jquery.
Here's the section of code that I'd like to have function in real time.
$('#grab').click(function() {
function getId(url) {
var regExp = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/;
var match = url.match(regExp);
if (match && match[2].length == 11) {
return match[2];
} else {
return 'error';
}
}
src = $('iframe').attr('src');
setInterval(function() {
src = $('iframe').attr('src');
}, 10000);
window.open('http://youtubeinmp3.com/fetch/?video=http://www.youtube.com/watch?v=' + getId(src), '_blank');
});
Relevant links
GitHub
Chrome Extension
Thank you for taking the time to read my question.
You're bad at explaining (and might want to edit the question to reflect what you want), but basically the problem is this:
You have a YouTube embed in the page, with a particular video ID in src.
When the video changes, that happens without updating the src (by using YT embed API).
Therefore, if you try to grab just the src, it's not the latest video but the first you loaded.
As an extension, I see two ways of trying to solve it:
You could try to initialize the YT API yourself to get a player reference. I don't know if it will break the code of Dubtrack.
You could inject a script in the iframe as well that would somehow extract the video being played in a way other than relying on src.
It's an open problem how to solve it, and the fact that you're basically providing "just" a bookmarklet may be an obstacle.

Text to speech using javascript and translate_tts

I have been trying to get my webpage to play up what it says in a text box when the user click on a link, but so far I haven't manage. I have tried with
function listen(){
var sound = new Audio();
sound.src = "http://translate.google.com/translate_tts?ie=UTF-8&tl=sv&q=Testar";
sound.play();
alert(":D");
return false;
}
and
function listen(){
var sound = document.createElement("audio");
sound.setAttribute("src","http://translate.google.com/translate_tts?tl=sv&q=Testar");
sound.load();
sound.play();
alert(":D");
return false;
}
I have tried adding ie=UTF-8 to the link, and tried both with and without sound.play(); but nothing have worked. I get smiley face from the alert so I know the function runs. Can someone please help me get this to work.
EDIT: I did a work around by using and iframe which I hide by using display: none; and then simply using javascript to change the src, not the best solution but it works... for now.
This is an easy way to do it :
var sound = new Audio("http://translate.google.com/translate_tts?tl=sv&q=Testar");
sound.play();
Now it's a bit more complicated, but still possible:
you need set up an user-agent string like a common browser and, more difficult, you also must provide a token into the GET request.
Some people managed to extract the token algorithm from the js code of the page, but it's quite a long work and at any time the algorithm changes you need to start again the reverse engineering of the cryptic code.
So it's much easier access to a particular url from the same site that generates an XHR that shows the token you need.
A simple script with phantomjs and grep will do the job for you, details here:
https://stackoverflow.com/a/37221340/6332793

JavaScript, Check a Radio Button on Load Depending on the End of URL

I am clutching at straws a little bit here, I want to do as the title says, when the page loads I would really love some javascript to fire off some code that checks the url, and reads the end of it. I then want to have an 'if' that says (URL ends with =en?check this radio:do nothing). the code I have attempting at fitting already is messy at best but I will show you to help you understand my lack of ability and to better explain my query:
window.onload = function(){
var url = window.location.href;
var lastPart = url.substr(url.lastIndexOf('/') + 1);
if (lastPart === "=en") {
radiobtn = document.getElementById("123456789");
radiobtn.checked = true;
}
};
Thank you guys!
This was a serious lack of understanding of the code of the code I was attempting to use.
var lastPart = url.substr(url.lastIndexOf('/') + 1);
I didnt realise that this was reading everything after the char '/'. After changing it to '=' it worked perfectly!
Sorry community!

Javascript: Creating a 'weird' conditional re-direct

If I had a normal website this would be a simple enough fix... but I've built my site on tumblr so I need a workaround. Every page runs off of the same code, so any solution script is going to run on every page.. can't quite figure this one out (did I mention I'm a total n00b?). There are lots of answers to questions LIKE this one, but I couldn't quite find the right syntax I suppose to answer this question...
The goal here is, if some goes to just the raw domain name, in this case milliondollarextreme.tv --> I'd like it to redirect to milliondollarextreme.tv/tagged/videos.
In any other case, by that I mean, if there is anything appended to the end of the domain name already, such as:
milliondollarextreme.tv/permalink/91298132843
milliondollarextreme.tv/tagged/blog
milliondollarextreme.tv/contact.htm
I don't want there to be any redirection going on. I only want the redirect to 'fire' really the first time the person types in the domain -- milliondollarextreme.tv
The trick here, the reason why I am asking (I did a search and 1000 apologies if this has been asked elsewhere, I just couldn't find it) is that the script has to run on every page, because it's hosted on tumblr, so every page is driven by the same code.
Any ideas? Thanks in advance.
This will simply redirect any visit to milliondollarextreme.tv/ to milliondollarextreme.tv/tagged/videos
if(window.location.pathname == '/')
{
window.location.pathname = '/tagged/videos';
}
However, it will do it every time they go to the root; like Gerardo, I'm not clear if that's what you want.
<script>
if( window.location.href == "http://milliondollarextreme.tv" ||
window.location.href == "http://milliondollarextreme.tv/" ||
window.location.href == "http://www.milliondollarextreme.tv" ||
window.location.href == "http://www.milliondollarextreme.tv/") {
window.location.href = "http://www.milliondollarextreme.tv/tagged/videos/";
}
</script>
What should happen when someone enters to http://milliondollarextreme.tv/ for the second time?

function call does not return to calling function

I'm using Shadowbox.js to display a slideshow on a website.
This slideshow shows several pictures and I would like to know who's looking at what pictures.
For this purpose I'm using statcounter.com.
Shadowbox offers a so called hook to call a function when the slideshow opens and when it changes to another picture.
I've written a small piece of code to get things moving, but for some reason, I get an entry in my statcounter log, but the shadowbox does not appear.
When I don't use the onopen and onchange in the options, the shadowbox does display.
As a test you can set up a directory where you place below code. Create to subdirs in this directory called "sb" and "pix". Get the Shadowbox-application from the website and store it in the "sb" directory (http://shadowbox-js.com/download.html).
Next to that store 3 testimages (called image1.jpg, image2.jpg and image3.jpg) in the "pix" directory.
To check if statcounter is picking up the pictures, you can use my testaccount on statcounter.com (just for viewing: account testcase, password casetest1).
Please find the html with the code here: http://www.heres-online.nl/test/index.html
Please take into account, I only just starting in javascript and html programming.
I can imagine I'm overlooking something terribly simple ...
Any help is highly appreciated.
Instead of trying to insert an image tag that way, just make one:
var img = new Image();
img.src = "... tracker URL ...";
That's all you need to do. edit Also get rid of all those backslashes in your URL strings; there's no point to them.
edit again I think this is all you need:
var nonsense = 1;
function tracker() {
var img = new Image();
img.src = "http://c.statcounter.com/counter.php?sc_project=5981755&security=582aa718&invisible=1&u=" +
encodeURIComponent("http://my.pix/" + Shadowbox.getCurrent().content) +
'&nonsense=' + new Date().getTime() + '_' + nonsense++);
return true;
}
(added a "nonsense" parameter to try and overcome possible caching issues)
edits — OK note the "return true" and the change of "escapeURIComponent" (wrong) to "encodeURIComponent". (I always get confused because the old deprecated function was called "escape".)
Please hold your horses on my last comment. I made a mistake myself (typo).
Instead of encodeURIComponent I typed enocdeURIComponent (why not copy/paste ... yeah, well I just didn't).
The script is now doing exactly what I intended it to do. I know have a Statcounter entry for every picture in the slideshow! Superb. I'm very pleased with your help, this was really nagging me, not being able to get it running. And the speed of getting an answer here was really amazing!
I've posted your solution on the Shadowbox.js forum as well. I posted my question there too, but no answers yet. But for anyone strugling with the same issue, this solution might be helpfull.
Thanks again, and have a nice weekend!

Categories

Resources