get the current URL

0

Very good I have created a plugin for chrome and it fails me to get the URL current and then work with it with just one button

in mainfest.json I have this:

{
"name": "Dom_Test",
"version": "1",
"manifest_version": 2,
"description": "Test de dominio con la herramienta newp, se podrĂ¡n obtener los resultados en PDF",
"permissions": [ 
  "tabs"
],
  "background": {
    "scripts": ["background.js"],
    "persistent": false
  },
    "browser_action": {
        "default_icon": "/imagenes/registro.png",
         "default_title": "Test de dominios",
        "default_popup":"popup.html"
    }
}

and popup.html as follows:

    <html>
<head>
    <meta charset="utf-8">
    <title>Test de dominios</title>

        <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/custom.css" rel="stylesheet">


      <!--  acciones de los js -->
      <script src="popup.js"></script> 



<style type="text/css"> 
body{
  min-width:360px;
}
a{
  color:#f90;
  text-decoration:none;
}
a:hover{
  text-decoration:underline;
}
p{
  font:0.8em sans-serif;
}
h1{
  font:1em sans-serif;
  color:#fff;
  background:#000;
  padding:5px
}
</style>
</head>
<body>
    <div><h1>TEST DE DOMINIO</h1></div>
    <p>
    <form>
<button name="button"  onclick="functionaunporcrear();">Botonazo</button>
<div id="host"> </div>
    </form>
    <hr>
    Pie
</body>
</html>

y en popup.js tengo lo siguiente:

    $(documento).ready(function (){
    chrome.tabs.get.Selected(null, function (tab)
    {
        var link = documento.createElement('a');
        link.href = tab.url;
        $('#host').html("Host : "+link.hostname);
    })  
});

I have helped with several guides and videotutorials but I can not get the current url

Once I have the url I will make a request to an address with that url but before I need the current URL

Thanks

    
asked by pablo 22.11.2018 в 12:24
source

1 answer

0

Through javascript you can obtain it in the following way:

var urlActual=window.location.href;

An example of working with OnClick:

function mifunction(){
  var urlActual=window.location.href;
  console.log("La url es:" + urlActual);
}
<html>
	<body>
		<button name="button" onclick="mifunction();">Botonazo</button>
	</body>
</html>

An example of the operation with event handler:

document.getElementById("botonazo").addEventListener("click", function(){
  var urlActual=window.location.href;
  console.log("La url es:" + urlActual);
});
<html>
	<body>
		<button name="button" id="botonazo">Botonazo</button>
	</body>
</html>

Example with handler and onload:

window.onload=function(){ 
  document.getElementById("botonazo").addEventListener("click", function(){
    var urlActual=window.location.href;
    console.log("La url es:" + urlActual);
  });
}
<html>
	<body>
		<button name="button" id="botonazo">Botonazo</button>
	</body>
</html>
    
answered by 22.11.2018 / 12:46
source