javascript to add source URL - javascript

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

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 end a script when is function is done?

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>

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>

Java Script redirect is not working in smarty .tpl file

I am developing a web application using PHP, Smarty combination, I have wrote a javascript redirect in the .tpl file, but it is not working now. Please take a look at the code and help me to find out the problem.
This is the code segment,
{literal}
<script type="text/javascript">
if(confirm({/literal}'{$info}'{literal})){
window.location = "logbook_client_section.php?e={$ckey}";
}
</script>
{/literal}
Problem is only in the redirect section, it is now redirecting to logbook_client_section_add.php?e={$ckey} , I would like to get the value of $ckey ( smarty variable) in the url.
try it
{literal}
<script type="text/javascript">
if(confirm('{/literal}{$info}{literal}')){
window.location = "oop.php?e={/literal}{$ckey}{literal}";
}
</script>
{/literal}
You are trying to use a {$var} inside a {literal} block. That cannot work...
The cleanest solution to avoid unreadable code caused by closing/opening the {literal} blocks within your code would be this:
<script type="text/javascript">
var info = '{$info}';
var ckey = '{$ckey}';
{literal}
if(confirm(info)){
window.location = "logbook_client_section.php?e=" + ckey;
}
{/literal}
</script>
You can use this instead:
var ckey='{$ckey}';
window.location = "logbook_client_section.php?e="+ckey;

Check URL for page name

I have some code that someone wants to put on their site. Here is the code i received from them:
<script language="JavaScript" src="http://dm4.contactatonce.com/scripts/PopIn.js" type="text/javascript"></script>
<script language="JavaScript" src="http://dm4.contactatonce.com/PopInGenerator.aspx?MerchantId=44542&ProviderId=3176&PlacementId=0" type="text/javascript"> </script>
<script language="JavaScript">
popIn();
</script>
The way this particular site is set up I cannot pick and choose which page to display it on - it has to go in the <head> of every page. The problem is that i want it to NOT show up on only one particular page. THe page name is /CreditApplication.aspx. I know i need to add an if statement to check the URL but i'm not quite sure how to accomplish that with this particular code as it uses external javascript files.
Any help would be great appreciated!
Thanks!
EDIT: Thanks for all the answers! Let me clarify one thing: the reason i need this is because the page the code is going on is a secured (https) page. These js scripts are not using secured links so in some browsers it gives you an error saying "some content on this page may not be secure" or whatever. I am trying to make sure these don't run on only this page. That's why i need the conditional statement on them. Hope that helps.
How about
if (! /CreditApplication\.aspx$/.test(window.location.href) {
popIn();
}
Edit the regex as needed if the page can accept parameters.
Try this:
<script>
window.location.pathname !== '/CreditApplication.aspx' &&
document.write(unescape('%3Cscript src="http%3A//dm4.contactatonce.com/PopInGenerator.aspx%3FMerchantId%3D44542%26ProviderId%3D3176%26PlacementId%3D0%22%20type%3D%22text/javascript')) &&
document.write(unescape('%3Cscript src="http%3A//dm4.contactatonce.com/scripts/PopIn.js"%3E%3C/script%3E'));
</script>
I'm not completely sure I'm following your question, but if I am here is how to get started:
if(window.location.href.indexOf("/CreditApplication.aspx") === -1) {
popIn();
}

Categories

Resources