Page Redirection - javascript

To help better explain my Question, here is what i have:
I am using a Precoded service, on it i have access to the Template HTML Files Only.
The URL to the Login page is : DynamicPage.aspx?Site=Mysite&WebCode=LoginRequired
The Main site URL would look like : DynamicPage.aspx?Site=Mysite&WebCode= or DynamicPage.aspx?Site=Mysite
What i need is a JavaScript I could put into the main header template file that would view "WebCode" And depending on whats entered redirect to a certain page.
I got from "Sitifensys" a code
Sitifensys
if (window.location.href!="foo.bar/login.php") window.location.href="login.php";
The problem with this code is even when i go to the main page it still redirects me to login.php which i do not want it to. I need this code to Read the "WebCode" If it is "LoginRequired" Redirect to "Login.php" else if redirect to "Test.php"
Hope this explanation is a bit better.

Try
window.location.href = "http://www.something.com/"

Don't do this that way. If you do anything in JavaScript it may be easily blocked by user. Better add and if() in your PHP code and then redirect to login, if user don't have specific session key:
if(!(isset($_SESSION['logged_in']) && true == $_SESSION['logged_in']))
{
header('Location: login.php');
die();
}

Just as answered earlier, you should add a window.location.href="somepage" somewhere.
Some where in your script :
if (window.location.href!="foo.bar/login.php") window.location.href="login.php";
This doesn't need to be in a listener for the page load event.
EDIT :
Regarding your new descriptionn I would use (but would not recommend;)) something like :
if (location.pathname.indexOf("WebCode=LoginRequired")>0) {
window.location.href="login.php";
}
Hope this will help.

if (location.pathname.indexOf(login.php) >= 0) {
//don't redirect
} else {
/...
}

You can add this script at the beginning of the body tag:
<body>
<script>
if((/WebCode\=LoginRequired/).test(window.location.href)){
window.location.href = window.location.href.
replace(/(\?|\&)WebCode\=LoginRequired/, '');
}
</script>
...
</body>

Related

Redirect page without onclick wordpress

i'm trying to hard to code some js in wordpress whereby if i type http://test.com/test it'll take me to http://test.com/#test.
I just don't want the user to see the # anchor when pressing the link (from an email or a message).
ideally i thought something like this may work, but i'm clueless..
if the link test.com/test
{
redirect to (test.com/#test)
}
without any onclick or any user input
Thank you in advance
if (window.location.href == 'http://test.com/test') {
window.location.replace("http://test.com/#test");
}
You can include this in Dom ready function.

JavaScript Alert on Specific Page

How can I alert in javascript but only on any one specific page suppose, I want to alert "Hey!" but the problem is same hey is coming on other pages too after site loading, I want that Hey alert should be alert on homepage of my site now other pages.
Is it possible or sounds like crazy?
Edit: from comments: I have theme of wordpress on which homepage i'm working so wordpress generate html on run time i never saw html of it
This is just for fun: (use in your case !)
if( window.location.href.indexOf('stackoverflow.com/questions/47197404/javascript-alert-on-specific-page') !== -1 ) {
alert('Hello world');
}
Ref: window.location, .indexOf()
What do you want to do?
Like to you want to have a specific JS file called on the home page?
So do a enqueue_script for the home, put this code in your functions or in the plugin file:
if(is_home()) {
function load_your_script() {
wp_register_script('your_script_name_here', PATH/TO/YOUR/JS/FILE/HERE);
wp_enqueue_script('your_script_name_here');
}
add_action('wp_enqueue_scripts', 'load_your_script');
}
So that way, the JS file will be only loaded in your home.

Detect if tag is in URL and then remove hidden class

I have a form on my contract form, which submits to a third party site and then I can define a URL to return the user to. I currently return the user to the same /contact page but I wanted to give them a message that it had submitted (since ajax forms don't work with the third party) and I don't want to have a whole page for it.
Therefore I had the idea to return the user to /contact#thanks
I have some code on my site which goes like this:
<div id="alert" class="hidden">Form Submitted. We will reply soon.</div>
Now I want a small bit of javascript on my page which detects if the URL has the #thanks tag on it, as above, and then removes the hidden class from my alert div. Is javascript able to detect this and if so, how do I go about it?
Include jquery and script. I test and work
$(document).ready(function(){
if(window.location.hash) {
$("#alert").show()
}
});
Siii = Yes use hash
I'm not totally sure that I've understood what are you trying to achieve, but this might help you:
if (window.location.hash === '#thanks') {
document.getElementById('alert').classList.remove('hidden');
}

How to make link with hidden URL

I was wondering whether it was possible to make a link with <a> tags that doesn't display its URL?
Put into other words, I would like a piece of HTML that either hides or obfuscates the URL that it links to.
I have found this StackOverflow question, but I'd prefer that the link would work in all browsers (not just chrome) and was not a popup. I already have access to jQuery, Bootstrap and PHP 5.5.
Any help would be appreciated.
UPDATE: I feel this needs clarification. I do not want it to be visible in ANY way - i.e. this is a link that must NOT be shared, so I cannot simply use redirects and just hide the URL when it is hovered over - I do not want it visible in the source code either. Sorry for any inconvenience :(
Thanks,
ICT
You can capture the link in a closure to hide it, then point the window there when the <a> is clicked, for example
function hideLink(anchor) {
var href = anchor.getAttribute('href');
anchor.removeAttribute('href');
anchor.className += ' pseudolink';
anchor.addEventListener('click', function (e) {
e.preventDefault();
window.location.href = href;
});
}
window.addEventListener('load', function () {
hideLink(
document.getElementById('my_link')
);
});
.pseudolink {
color: blue;
cursor: pointer;
text-decoration: underline;
}
<a id="my_link" href="http://google.com">Hover over me</a>
It is not possible to completely hide the URL you are attempting to navigate to. The URL must be present in some form - such as the 'href' attribute of the <a> - tag to tell the browser where to navigate to.
However, it is possible to mask the URL with access to your server settings. Using a .htaccess file it is possible to redirect from one entered URL to another, whilst maintaining the entered URL within the address bar of the browser. There are many sources online that explain how to do this.
Simply handling each link using some logic within a PHP file may be suitable to hide the link in the source. For example, you could send every link to handler.php and specify a value for which page to navigate to, ie handler.php?page=1.
handler.php would then contain something along the lines of:
<?php
if ($_GET['page'] == 1) header('Location: /where/you/want/to/go');
if ($_GET['page'] == 2) header('Location: /where/else/you/want/to/go');
?>
This way, the user will not know where the link actually goes and (using the .htaccess settings) unaware of the URL they have navigated to.
You could use an url minifier like this one : https://goo.gl/.
<a data-link="some link here"></a>
$('a').on('click', () => {
window.location.href = $(this).attr('data-link');
});
User won't see the link while hovering it.

YOURLS Redirect with Javascript

Currently I am using YOURLS PHP URL shorterning script.
In their functions.php, seems like they support both redirection which is with PHP header location and javascript.
Functions.php
I want to redirect with javascript, which will show me the message before redirect. I found this coding inside functions.php
// Redirect to another page using Javascript. Set optional (bool)$dontwait to false to force manual redirection (make sure a message has been read by user)
function yourls_redirect_javascript( $location, $dontwait = true ) {
if( $dontwait ) {
echo <<<REDIR
<script type="text/javascript">
window.location="$location";
</script>
<small>(if you are not redirected after 10 seconds, please click here)</small>
REDIR;
} else {
echo <<<MANUAL
<p>Please click here</p>
MANUAL;
}
}
But I don't know how to do it. Is that coding currently commented or ? What should i change to redirect with javascript ?
Please help me out. Thanks.
The function is all ready to go. A javascript redirect will occur on the client side, so this script outputs the appropriate javascript. Call it like so:
<?php # http://www.myurl.com/was/here.php
yourls_redirect_javascript('http://www.myurl.com/go/here.php');
?>
When this page loads javascript will be used to redirect the user. If the javascript fails, there will be a link for them to click to follow the redirect.
I suspect that the "here document" syntax is throwing you off a bit. Read the PHP Docs to learn more about echo <<<
http://php.net/manual/en/function.echo.php

Categories

Resources