How to end a script when is function is done? - javascript

I have a Blogger Blog, and I want to setup a link that auto open when someone visit me. I found some scripts that works on this site.
But I don't know how to stop the script when its function is done. Any suggestion? I'm totally a noob with this language.
This is the script what I have choose:
<script language="javascript" type="text/javascript">
<!--
window.setTimeout('window.location="example.blogspot.com"; ',2000);
// -->

document.AddEventListener("DOMContentLoaded","CallThis");
function CallThis(){
window.location="www.blah.com";
}

Please elaborate,
Do you want to redirect the user to a different page or do you want to redirect him to the same page and that puts you in a loop ?
If you want to redirect a user to a different page the second the page loads use the following command :
<script language="javascript" type="text/javascript">
window.onload = function() {
window.location.replace("http://someplaceamazing.com");
}
</script>
if you want to redirect a user to the same page (reload the page) but not reload it everytime, just a single time use this :
<script language="javascript" type="text/javascript">
window.onload = function() {
if(!window.location.hash) {
window.location = window.location + '#loaded';
window.location.reload();
}
}
</script>

Related

Set value of input field after retrieving it using an external script (HTML dom refresh ?)

What I want to do
Get a form using an external script. Prefill and disable some fields. Example:
How I do it
I retrieve a form using an external script(s) :
<!-- Lumesse js imports -->
<!--Apply-->
<script type="text/javascript"
src="https://emea3.recruitmentplatform.com/apply-app/static/apply/release/2-LATEST/apply-preloader-namespaced.js"
th:src="#{https://emea3.recruitmentplatform.com/apply-app/static/apply/release/2-LATEST/apply-preloader-namespaced.js}"
data-lumesse-apply=""
data-lumesse-apply-config-key="XXX"
data-lumesse-apply-host="https://emea3.recruitmentplatform.com"
data-lumesse-apply-description-placement="bottom"
data-lumesse-apply-menu="top"
data-lumesse-apply-menu-placement="top"
data-lumesse-apply-repeatable-style="on-demand">
</script>
<script src="https://emea3.recruitmentplatform.com/apply-
app/static/apply/release/2-LATEST/apply-application-form-namespaced.js"
th:src="#{https://emea3.recruitmentplatform.com/apply-
app/static/apply/release/2-LATEST/apply-application-form-namespaced.js}">
</script>
<!-- in comment because otherwise conflict with static/vendor/jquery.min.js -->
<!-- <script src="https://code.jquery.com/jquery-1.12.3.min.js"
th:src="#{https://code.jquery.com/jquery-1.12.3.min.js}"
th:integrity="sha"
th:crossorigin="anonymous"></script>-->
<script src="https://emea3.recruitmentplatform.com/apply-
app/static/themes/release/latest/silk/js/main.js"
th:src="#{https://emea3.recruitmentplatform.com/apply-
app/static/themes/release/latest/silk/js/main.js}"></script>
The code for prefill or disable works fine.:
<!-- custom javascript -->
<script type="text/javascript" th:inline="javascript" >
$(document).ready(function() {
// fix menu when passed
$('.masthead').visibility({
once : false,
onBottomPassed : function() {
$('.fixed.menu').transition('fade in');
$('.navbar-fixed-top').css({'top': '52px'});
},
onBottomPassedReverse : function() {
$('.fixed.menu').transition('fade out');
$('.navbar-fixed-top').css({'top': '75px'});
}
});
// create sidebar and attach to menu open
$('.ui.sidebar').sidebar('attach events', '.toc.item');
window.setTimeout(function(){
preFillForm();
},0);
});
function preFillForm(){
var gegevens = [[${gegevens}]];
for(var id in gegevens){
$("#" + id).val(gegevens[id]);
}
$("#first_name_1").attr('disabled','disabled');
$("#last_name_2").attr('disabled','disabled');
$("#e-mail_address_3").attr('disabled','disabled');
}
</script>
The problem
My fields don't get prefilled .. My guess is because they don't get reloaded or get overwritten ..
First possible solution
After many hours I added a setTimeout of 0. This seems to work. Now I discovered that this only works on a page refresh or when the user already visited the site and did a page refresh before ...
So still when a user go to the site for the first time it doesn't work ..
Any help would be much appreciated
Cheers
(PS: setTimeout of more seconds is not the way I want to go ..)
UPDATED
My application is a Spring Boot App. Working with Thymeleaf. Also use of Semantic and Jquery in FrontEnd.
Try using sessionStorage or localStorage (sessionStorage seems to be better in this situation).
https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage

How to close web form using javascript vb.net asp.net

This is my scenario. If the session is expired in our system/application it is redirecting to the log-in page.
If i open the application on two different browsers,If session is timed out in one of the broswer window,it must close the other window too.In my case ,i can still process the 2nd browser/window(i can click buttons and etc.,).
Please suggest me what i can do here
<pre>
<code>
This is my java script code:
<script type="text/javascript">
<!--
function closeWindow() {
self.close();
}
// -->
</script>
This is my server side code
If Not clsUserSession.IsSessionExist(getUserid, False) Then
Me.ClientScript.RegisterStartupScript(Me.GetType(), "CloseWindowScript", "closeWindow();", True)
Exit Sub
End If
This is my java script code:
<script type="text/javascript">
<!--
function closeWindow() {
self.close();
}
// -->
</script>
Hope thses will help you
Try with www3school
Window close function api

If <div id='credits'> Removed From HTML Document Then Redirect to a New Web Page

If a specific <div id='credits'> is removed from the HTML document, I want it the user to be redirected to a new web page using JavaScript.
For example:
<div id="credits"></div>
If someone removes it then users will be automatically redirected to my website.
This is to protect copyrights.
If I understand you are worried about someone copying your page to their website, you can try this, though it is indeed absurdly easy to get around:
<script type="text/javascript">
if($('#credits').length == 0)
window.location = 'http://www.url.com'
</script>
in your page load event you may check like following:
$(document).ready(function(){
var yourDiv=document.getElementById("credits");
if(yourDiv===null || yourDiv===undefined)
window.location="http://www.google.com";
});
or
window.onload=function(){
var yourDiv=document.getElementById("credits");
if(yourDiv===null || yourDiv===undefined)
window.location="http://www.google.com";
}
Try this:
<script type="text/javascript">
$(document).ready(function(){
if($("#your-div-id").length == 0) {
window.location.href = "your-url";
}
});
</script>

javascript to add source URL

I am using the following code in CEWP (content editor webpart) so after taking a survey user will be redirected to thank you page. the redirecting is not working. Am I missing something? (user able to take survey and newform.aspx never closes)
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script type="text/javascript">
function PreSaveAction(){
var URL = location.pathname.replace("NewForm.aspx","/surveys/Pages/ThankYou.aspx");
if(GetUrlKeyValue("IsDlg")==="1"){
URL+="?IsDlg=1";
}
$("#aspnetForm").attr("action",location.pathname+"?Source="+URL);
return true;
}
</script>
Where's the redirect? Have you tried using window.location(URL); ?
Means you are getting double "/" in URL like this :
/surveys/Lists/TestSurvey//surveys/Pages/ThankYou.aspx?IsDlg=1
just remove starting / from below string
/surveys/Pages/ThankYou.aspx

Why does fb:fan control disappear after login with requireSession on external page?

Reqs:
I want to make an external page with a 'Become a Fan'-button.
I want users to login to facebook once they open the page (this is because of other features in the site)
So I call FB.Connect.requireSession once the page loads, and put a <fb:fan></fb:fan> control on the page.
Then when I open the page I see the 'Connect with Facebook to Continue' pop-up and a small 'Become a fan' button. This seems right. When I log in to Facebook using the pop-up the 'Become a fan button' disappears!!
Can anyone explain this weird behaviour or does someone know a way to reach my requirements in a different way.
Below you can see my code.
Thanks in advance!
<!-- Head -->
<script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php/nl_NL" type="text/javascript"></script>
<script type="text/javascript">
var fbApiKey = "MY_KEY";
var fbXdReceiver = "xd_receiver.htm";
function fbLoad() {
FB.init(fbApiKey, fbXdReceiver);
FB.ensureInit(function() {
FB.Connect.requireSession(null, null, false);
});
}
</script>
<!-- Body onload="fbLoad();" -->
<fb:fan profile_id="24932281961" name="Nutella" id="fanButton" stream="0" connections="0" logobar="0" width="250"></fb:fan>
I solved this by making the page refresh after a successful login
So I call requireSession with a callback
FB.ensureInit(function() {
FB.Connect.requireSession(requireSessionSuccess, null, false);
});
And this is the code for the callback
function requireSessionSuccess() {
window.open('default.aspx?fbloggedin=true', '_self');
}

Categories

Resources