Javascript redirectionament - javascript

Hi guys its m first question here y needd to check the actual url and then depending if it is ?lang=es or ?lang=en redirect to https://regular.autobusing.com/paqtur/venta?locale=es or =en clicking on a button with an id=vivesegovia
thats what ive done but its not working
<script type="text/javascript">
function lanzacompra(){
var URLactual = window.location.href;
document.getElementById("viveboton").onclick = function () {
if (URLactual == "http://lasepulvedana.es/testweb/sepulvedana/vive-segovia.php?lang=en")
{
window.location ( "https://regular.autobusing.com/paqtur/venta?empresa=lasepulvedana&locale=en");
}
else
{
window.location ( "https://regular.autobusing.com/paqtur/venta?empresa=lasepulvedana&locale=es");
}
}/*onclic*/
}/*lanzacompra*/
</script>
Thank you very much for your help in advance guys :)

you need to set it by assignment, not as parameter
replace
window.location ( "https://regular.autobusing.com/paqtur/venta?empresa=lasepulvedana&locale=en");
by
window.location.href = "https://regular.autobusing.com/paqtur/venta?empresa=lasepulvedana&locale=en";

Related

Multiple Pages in one HTML file/page, with more than 2 pages

I know that the <script> element can have function show(shown, hidden) on it. but with the 2 pages ({document.getElementById(shown).style.display='block'; document.getElementById(hidden).style.display='none'; return false;) in that, I can't figure out how to make that page count more. Any help?
P.S. I am open to almost anything. I can't guarantee your answers will help, but I might be able to figure it out using your suggestions.
I have tried more things on the function show(shown, hidden, hidden, hidden) but that does not help.
I am stuck. I have researched anything I could find. I can't figure it out.
Please help me.
My specific code I want suggestions on is this:
<script>
function show(shown, hidden) {
document.getElementById(shown).style.display='block';
document.getElementById(hidden).style.display='none';
return false;
}
</script>
with some <div>s.
I know this is probably not helping you figure out how to help me, but I need to know. (I hate full-on JavaScript!)
<!doctype html>
<html lang="en">
<head>
<title>Multi but Single Page</title>
<meta charset="utf-8">
<style>
.templates {
display: none;
}
</style>
<script>
// we save all templates in an global Variable
var templateStack = [];
// https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript
function getParameterByName(name, url) {
url = url || window.location.href;
name = name.replace(/[\[\]]/g, '\\$&');
var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, ' '));
}
window.addEventListener('load', function (e) {
// get all hidden template elements
var templates = document.getElementsByClassName('templates');
for (var i = 0, v; v = templates[i]; i++) {
// each Child Node is a new Page
for (var j = 0, x; x = v.childNodes[j]; j++) {
// at least if it's an element
if (x.nodeType === Node.ELEMENT_NODE) {
templateStack.push(x);
}
}
}
// uri support ?page=1 loads Page 2 and ?page=0 loads Page 1, default is 0
var pageIndex = getParameterByName('page') || '0';
// so we can test it with a Browser by just insert 'loadPage(1)'
loadPage(pageIndex);
});
function loadPage(index) {
// only valid indexes
if (index >= templateStack.length || index < 0) {
document.body.innerText = '404 Page not found';
return;
}
// clean everything in our page
document.body.innerHTML = '';
// append our fetched Page out of our Global Variable
document.body.appendChild(templateStack[index]);
}
</script>
</head>
<body>
<div class="templates">
<div>
<h3>Page 1</h3>
<p>
Welcome to Page 1
</p>
Load Page 2
</div>
<div>
<h1>Page 2</h1>
<p>
Our Page 2
</p>
Back to Page 1
</div>
</div>
</body>
</html>
I understand that you can use it with 2 pages but when you want to make more pages like 4-5 pages?
First you need an clear function (it will hide all the pages)
In the clear function get the body in dom and get all the childrens then make a foreach loop hiding all of them
Second you need an show function which will use the page as an parameter like "show('page1');" it will first call the clear function and then show the page1

How Render Partial view from JS in Layout

Trying to use JS to render a partial view based on the URL... This is almost working but I can't use Razor syntax in the PathRoot Var so I tried escaping it but then it doesn't render... Any Ideas would be greatly appreciated.
<head>
<script>
var SiteUrl = window.location.hostname;
var FloodlightPathRoot = '##Html.Partial("~/Views/shared/FloodlightTags/' ;
var FloodlightEnd = '");';
switch (SiteUrl) {
case "www.website.com":
var FloodlightFilePath = "first.cshtml";
break;
case "www.website2.com":
var FloodlightFilePath = "second.cshtml";
break;
}
var FloodFull = (FloodlightPathRoot + FloodlightFilePath + FloodlightEnd);
$('head').append(FloodFull);
</script>
</head>
It writes the razor syntax to the head but the page has already rendered. Can I render the razor syntax after the head is updated?
<head>
#Html.Partial("~/Views/shared/FloodlightTags/first.cshtml");
</head>
While using your code it can done this way
<head>
<script>
#{
string path = Request.RequestUri.PathAndQuery;
if (path.Contains("www.website.com"))
{
#Html.Partial("~/Views/shared/FloodlightTags/first.cshtml");
}
else if (path.Contains("www.website.com"))
{
#Html.Partial("~/Views/shared/FloodlightTags/second.cshtml");
}
}
</script>
</head>
in addition to Stephen's answer you can do it like this:
#{
var url = new Uri(new Uri(this.Context.Request.Url.GetLeftPart(UriPartial.Authority)), Url.Content("~/")).ToString();
if (url == "http://www.website.com/")
{
#Html.Partial("~/Views/shared/FloodlightTags/first.cshtml");
}
else if (url == "http://www.website2.com/")
{
#Html.Partial("~/Views/shared/FloodlightTags/second.cshtml");
}
}
var url will get the site url and then it will check weather it matches http://www.website.com/ if so it will render first.cshtml else it will check the second condition.

How to find the script path from his own js file

I want to find the script path of my own js file in itself.
So I want have as a string "C:\files\MyProject\MyScripts\MyJavaScript.js".
How is that possible?
You can try (Jquery):
var myScriptDetails = $('script');
myScriptDetails will contain details regarding the script, including its location.
Try this solution. I think this is exactly what u want :)
Put this code in each of your linked script file
var scriptEls = document.getElementsByTagName( 'script' );
var thisScriptEl = scriptEls[scriptEls.length - 1];
var scriptPath = thisScriptEl.src;
var scriptFolder = scriptPath.substr(0, scriptPath.lastIndexOf( '/' )+1 );
console.log(scriptPath +" "+ scriptFolder );// you can save these in any variable also
I tested it with this HTML code:
<!DOCTYPE html>
<html>
<head>
<title>testing...</title>
<script type="text/javascript" src="test.js"></script>
<script type="text/javascript" src="js/test2.js"></script>
<script type="text/javascript" src="../test3.js"></script>
</head>
<body>
content area
</body>
</html>
And found this Output in console:
file:///D:/workspace/dbshell/www/test.js file:///D:/workspace/dbshell/www/ test.js:6
file:///D:/workspace/dbshell/www/js/test2.js file:///D:/workspace/dbshell/www/js/ test2.js:6
file:///D:/workspace/dbshell/test3.js file:///D:/workspace/dbshell/ test3.js:6
Special thanks to meouw..
Hope this helps..
here is how I made it:
function ScriptPath() {
var scriptPath = '';
try {
//Throw an error to generate a stack trace
throw new Error();
}
catch(e) {
//Split the stack trace into each line
var stackLines = e.stack.split('\n');
var callerIndex = 0;
//Now walk though each line until we find a path reference
for(var i in stackLines){
if(!stackLines[i].match(/http[s]?:\/\//)) continue;
//We skipped all the lines with out an http so we now have a script reference
//This one is the class constructor, the next is the getScriptPath() call
//The one after that is the user code requesting the path info (so offset by 2)
callerIndex = Number(i) + 2;
break;
}
//Now parse the string for each section we want to return
pathParts = stackLines[callerIndex].match(/((http[s]?:\/\/.+\/)([^\/]+\.js)):/);
}
this.fullPath = function() {
return pathParts[1];
};
this.path = function() {
return pathParts[2];
};
this.file = function() {
return pathParts[3];
};
this.fileNoExt = function() {
var parts = this.file().split('.');
parts.length = parts.length != 1 ? parts.length - 1 : 1;
return parts.join('.');
};
}
Client-side you can't access the physical path. You can get the src attribute of the script tag though.
Server-side you can get the physical path. For example (C#):
Path.GetFullPath(path)
For finding full path of URL in JavaScript use this location.href
If you can define absolute path of server as constant variable,
you can do it by casting from window.location.href. remove host prefix window.location.host from window.location.href and prepand with server's absolute path.
Try:
console.log(window.location.href);

Refresh page on button click and after that call an function

I am trying to refresh an web page on button click and after page refresh call an function.
But its reverse is not happening i.e. first call the function and after that refresh the page.
This is what I have tried:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
function restartall(){
$('#text').append('hello');
}
</script>
<a href="javascript:document.location.reload();">
<button onclick="restartall()">Referash Map</button></a>
<div id="text"></div>
please solve this query.
use below code
<body onload="onrefresh();">
<a href="javascript:myfun();">
function myfun(){
window.location.search += '&reload=true';
}
function onrefresh(){
if(gup("reload")=='true'){
// DO whatever want to do.
}
}
function gup( name ){
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}
<a href="javascript:abc();"><script>function restartall() {
$('#text').append('hello');
}
function abc() {
document.location.reload();
restartall();
}</script>
When you reload page you're loosing JavaScript call stack. You would have to append some parameter to URL or use cookie.
try this
<script>
function restartall(){
$('#text').append('hello');
document.location.reload();
}
</script>
<input type="button" onclick="restartall()" value="Referash Map" />
<div id="text"></div>

how can I get asp.net aspx page name from javascript?

Trying to get the aspx name from the current page from javascript?
Try this
<script language="javascript">
var url = window.location.pathname;
var myPageName = url.substring(url.lastIndexOf('/') + 1);
alert(myPageName);
</script>
As an alternative -
var page = '<%=Path.GetFileName(Request.Path)%>'
Do you want to parse the current window, or rely on asp.net?
Both work - it's up to you.
To Get The Exact Formname using jquery one can do string manipulation as shown.
$(document).ready(function () {
var path = window.location.pathname;
var page = path.substr( path.lastIndexOf("/") + 1 ).split(".",1);
var formname=page[0].toString();
alert(formname);
});
This one without the query string
$(function () {
var url = window.location.pathname;
var pageQuery = url.substring(url.lastIndexOf('/') + 1);
var page = pageQuery.split('?')[0];
)}

Categories

Resources