JavaScript keeps reloading my page - javascript

I am running this script on my page to replace mc= to MediaCode= in my url, the script works but it keeps reloading the page over and over.
This is the script:
<script>location.href = location.href.replace('mc=', 'MediaCode=')</script>

when you put
location.href = someHref
you make js to reload the page. Then, I gues, it meets that line again and continues reloading

Why not put this line into an if condition? This will check whether the URL contains the desired string or not and reload based on that:
const mcQueryParam = /mc\=/;
if(location.href.match(mcQueryParam)){
location.href = location.href.replace(mcQueryParam, "MediaCode=");
}

The solution to this is a slight alteration to Xufox's solution
<script type="text/javascript">$(document).ready(function () {
if(window.location.href.indexOf("mc=") > -1) {
location.href = location.href.replace('mc=', 'MediaCode=')
}});
Many thanks to Xufox

Related

How to make all website dead links redirect to particular page via JS?

Here's the issue:
Got multiple internal links on the web, still not connected to actual html files.
Im working with local files for the time being
Wanna make js redirect all of the dead links to one particular page (let's call the file: 404-error-page.html) up until I will finish the rest of html files to make those dead links active again
Purpose: wanna keep user away from seeing 404 blank page and instead show em some temporary page (404-error-page.html)
Sorry if that's messy - 1st time adding a question here.
HTML
<html><body><a href="random-link-directing-to-a-non-existing-page"></body></html>
JS
$('a').on('click', function(event) {
var $element = $(event.target);
var link = "404-error-page.html";
if(result.broken) {
if(result.http.response && ![undefined, 500].includes(result.http.response.statusCode)) {
event.preventDefault();
document.location.href = link;
}
}
});
I've already tried some alterations of this code but it's not working for me.
Firstly, need to make this functional on local files and then ofc online.
Any ideas?
Set the data-dead-link attribute to all your unfinished link tag as the follow.
Correct Link
<a data-dead-link>Dead Link</a>
<a data-dead-link>Dead Link</a>
Before the </body> tag insert the follow script
var deadLinks = document.querySelectorAll(`[data-dead-link]`)
deadLinks.forEach(el => {
el.href = "404-error-page.html"
})

jQuery keep selected content on load

I'm working on a control panel, and I need to have the output of a command appear live. I have managed to get this to work by having the command output to a file, and loading that file via jQuery. I refresh the output every second using:
<script>
$(function(){
setInterval(refreshFrame, 1000);
});
</script>
and
<script>
document.getElementById("title").innerHTML="Update";
function refreshFrame(){
$("#frame").load("/assets/readfile.php?type=custom#content")
}
</script>
This works and loads the output without refreshing the entire page. However this makes copying impossible as the selection is gone or gets mutated as the contents are loaded again. Is there any way to keep the selection across reloads, and if not, how can I pause the refreshing when there is a selection?
try something like this:
$(function(){
document.getElementById("title").innerHTML="Update";
setInterval(function(){
if(!$('#frame').is(":hover")){
$("#frame").load("/assets/readfile.php?type=custom#content")
}
}, 1000);
});
How about only updating the element's html if the content has changed and the user has not selected anything in #frame?
var lastContent;
$(function(){
setInterval(refreshFrame, 1000);
});
function refreshFrame(){
$.ajax({
url:"/assets/readfile.php?type=custom#content"
}).done(function(content) {
if(!$(window.getSelection().anchorNode).is("#frame") && $(window.getSelection().anchorNode).closest("#frame").length == 0 && content != lastContent) {
$("#frame").html(content);
lastContent=content;
}
});
}

if previous page url with “-cn” ending then change content in current page

I am making a site in both English and Chinese language, now I need to detect if previous page url with "-cn" ending (so will know it is chinese page) then change some content in current page(default is in English). Thank you.
code below, sorry I'm too new to JS
$(document).ready(function() {
var referrer = document.referrer;
//*detect if previous url without "-cn"ending do nothing if with "-cn" then below
//*change content inside to "new"<div class="block"></div>
});
<div class="block">old</div>
Try this one: i think you want something like this.
$(document).ready(function() {
var referrer = document.referrer;
if(referrer.indexOf("-cn")>-1)
{
// referrer url contain the "-cn"
}
else
{
// Not contain "-cn"
}
});

JQuery JavaScript, reload page before changePage, doesn't work as expected

I'm trying to force an update of my jQuery page before I want to change the page.
Code looks like this:
function popupOrRedirect2() {
location.reload();
var content = document.getElementById('invisibleDiv').innerHTML;
if (content > 0) {
$.mobile.changePage("http://localhost:8080/application/test");
} else {
$("#popupDialog4").popup("open");
}
}
I need to read a value from a hidden div, but the div gets updated only after the page is getting reloaded. I must ensure to have the latest value.
The problem is, it doesn't work. If I remove the location.reload() it works... but it doesn't have the newest value.
Any hint on how to achieve the behavior that I want?
I'm using jQuery mobile 1.8.3.
A quick and dirty way might be to use URL params to determine whether to reload.
Instead of:
location.reload()
you could do:
if (location.search !== '?reloaded=1') {
location.search = "reloaded=1"; //triggers a reload once
}

Function to find string on page and click link next to it?

I'm wondering whether it is possible to devise a script which will search a webpage for a certain string of text, and then click the link in the element id directly to its right.
Is this possible. Maybe javascript, php?
Please help, and thanks to all that do. :)
#Four_lo
Thanks for your reply. I'm sorry, maybe it's because I'm pretty new to javascript, but I can't really understand anything on the page you suggested.
I put together some javascript which will search the page for an element id and click the link within there.
<html>
<head>
<script type="text/javascript">
function init(){
var linkPage = document.getElementById('linkid').href;
window.location.href = linkPage;
}
onload=init;
</script>
</head>
<body>
GO HERE
I WANT TO CLICK HERE!
</body>
</html>
So basically, I need to search the page for GO HERE. Then, once this is found, I need to click the link in id="thisone", if that makes sense.
The above code works, and clicks the link within the id specified. However, I'd like to find certain text within that id, then move onto the next id, and click the link within that id.
It is possible. It will probably take some finesse but here is where you should start to access String you need. I believe regular expressions will be a must as well.
http://dom.spec.whatwg.org/#processinginstruction
http://domparsing.spec.whatwg.org/
Slightly more complicated than it needs to be:
function performAfterLinkWithText(text, perform) {
// get all the links
var $links = document.getElementsByTagName('a');
// scan them for your text
for(var i in $links) {
if($links[i].innerHTML === text) {
var $next = $links[i] // ready for loop
, terminateAfter = 20 // don't repeat forever
;
// keep checking the adjacent element
// because newlines show up as #text
do {
$next = $next.nextSibling;
} while( !$next.href && terminateAfter-- > 0 );
// do your thing
perform($next.href, $next); // window.location.href = $next.href;
}
}
}
// test -- performAfterLinkWithText('GO HERE', function(url, link) { console.log(url, link); });
performAfterLinkWithText('GO HERE', function(url) { window.location.href = $next.href; });
Or with jQuery:
window.location.href = $('a:contains("GO HERE")').next().attr('href')

Categories