This repository has been archived by the owner on Aug 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjuicy-redirect.html
70 lines (66 loc) · 2.37 KB
/
juicy-redirect.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<!-- juicy-redirect version:0.6.0 | MIT License -->
<script>
(function (global) {
var JuicyRedirectElementPrototype = Object.create(HTMLLinkElement.prototype);
JuicyRedirectElementPrototype.createdCallback = function(){
Object.defineProperty(this, 'url',{
set: function(val){
this.setAttribute('url', val);
},
get: function(){
return this.getAttribute('url');
}
})
};
JuicyRedirectElementPrototype.attachedCallback = function(){
this.redirect( this.getAttribute("url") );
};
/**
* @event juicy-redirect-pushstate
* Called whenever `history.state` is changed.
* @property {String} url new URL
*/
JuicyRedirectElementPrototype.attributeChangedCallback = function (attributeName, oldVal, newVal) {
switch(attributeName) {
case "url":
if(document.contains(this)){
this.redirect(newVal);
}
break;
}
};
JuicyRedirectElementPrototype.redirect = function(url){
if(!url){
return false;
}
if(url == "current") {
url = window.location.href;
}
var target = this.getAttribute("target");
if(target && target !== '_self'){
window.open(url, this.getAttribute("target"));
} else if(this.hasAttribute("history")) {
history.pushState(null, null, url);
this.dispatchEvent(
new CustomEvent(
"juicy-redirect-pushstate",
{
"detail":{"url":url},
"bubbles": true
}
)
);
}
else {
window.location = url;
}
this.setAttribute("url", "");
this.dispatchEvent(new CustomEvent('url-changed',{detail: ""}));
return url;
};
global.JuicyRedirectElement = document.registerElement('juicy-redirect', {
prototype: JuicyRedirectElementPrototype,
extends: "link"
});
})(window);
</script>